Angular 2+ 数组赋值奇怪的错误?
Angular 2+ array asignment weird bug?
问题很简单,但我一直没能找到答案。
当我尝试将对象数组的一个元素重新分配给另一个符合描述的对象时,没有任何反应,但是当我首先将该元素设置为 null 然后重新分配它时,它起作用了。
这是我正在使用的对象列表:
servers = [
{
instanceType: 'medium',
name: 'Production',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'large',
name: 'User Database',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Development Server',
status: 'offline',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Testing Environment Server',
status: 'stable',
started: new Date(15, 1, 2017)
}
];
以下是行不通的方法:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort'
})
export class SortPipe implements PipeTransform {
transform(value: any, args?: any): any {
for (const i of value) {
for (const j of value.slice(value.indexOf(i) + 1)) {
if (i.name > j.name) {
value[value.indexOf(i)] = j;
value[value.indexOf(j)] = i;
}
}
}
return value;
}
}
下面是行之有效的方法:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort'
})
export class SortPipe implements PipeTransform {
transform(value: any, args?: any): any {
for (const i of value) {
for (const j of value.slice(value.indexOf(i) + 1)) {
if (i.name > j.name) {
const index1 = value.indexOf(i);
const index2 = value.indexOf(j);
value[index1] = null;
value[index2] = null;
value[index1] = j;
value[index2] = i;
}
}
}
return value;
}
}
这不是一个严重的问题,但我现在很好奇为什么它不是以一种方式工作,而是以另一种方式工作。
感谢您的宝贵时间!
编辑 1: 将 (i.name[0] > j.name[0]) 更改为 (i.name > j.name) 的一致性。两次检查给出了相同的结果。
索引,i.name[0]
它在您实现旧式 for 循环时使用。即 (for(var i=0, i > length, i++)
。
但是for (const i of value)
是一个内置方法,当你调用i
时它已经有了值。
当你这样做时
value[value.indexOf(i)] = j;
value[value.indexOf(j)] = i;
value.indexOf(j)
在第二行 returns 之前的 value.indexOf(i)
是什么,因为您已经将 j
放入该位置。所以你最终得到
value[value.indexOf(i)] = i;
这是一个空操作。
如果循环时需要数组值和索引一起,可以使用
for (let [index, value] of array.entries())
而不只是 for..of
此外,如果您只是按 name
对 servers
数组进行排序,使用内置方法可能会容易得多:
value.sort((x, y) => x.name.localeCompare(y.name))
问题很简单,但我一直没能找到答案。 当我尝试将对象数组的一个元素重新分配给另一个符合描述的对象时,没有任何反应,但是当我首先将该元素设置为 null 然后重新分配它时,它起作用了。 这是我正在使用的对象列表:
servers = [
{
instanceType: 'medium',
name: 'Production',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'large',
name: 'User Database',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Development Server',
status: 'offline',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Testing Environment Server',
status: 'stable',
started: new Date(15, 1, 2017)
}
];
以下是行不通的方法:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort'
})
export class SortPipe implements PipeTransform {
transform(value: any, args?: any): any {
for (const i of value) {
for (const j of value.slice(value.indexOf(i) + 1)) {
if (i.name > j.name) {
value[value.indexOf(i)] = j;
value[value.indexOf(j)] = i;
}
}
}
return value;
}
}
下面是行之有效的方法:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort'
})
export class SortPipe implements PipeTransform {
transform(value: any, args?: any): any {
for (const i of value) {
for (const j of value.slice(value.indexOf(i) + 1)) {
if (i.name > j.name) {
const index1 = value.indexOf(i);
const index2 = value.indexOf(j);
value[index1] = null;
value[index2] = null;
value[index1] = j;
value[index2] = i;
}
}
}
return value;
}
}
这不是一个严重的问题,但我现在很好奇为什么它不是以一种方式工作,而是以另一种方式工作。 感谢您的宝贵时间!
编辑 1: 将 (i.name[0] > j.name[0]) 更改为 (i.name > j.name) 的一致性。两次检查给出了相同的结果。
索引,i.name[0]
它在您实现旧式 for 循环时使用。即 (for(var i=0, i > length, i++)
。
但是for (const i of value)
是一个内置方法,当你调用i
时它已经有了值。
当你这样做时
value[value.indexOf(i)] = j;
value[value.indexOf(j)] = i;
value.indexOf(j)
在第二行 returns 之前的 value.indexOf(i)
是什么,因为您已经将 j
放入该位置。所以你最终得到
value[value.indexOf(i)] = i;
这是一个空操作。
如果循环时需要数组值和索引一起,可以使用
for (let [index, value] of array.entries())
而不只是 for..of
此外,如果您只是按 name
对 servers
数组进行排序,使用内置方法可能会容易得多:
value.sort((x, y) => x.name.localeCompare(y.name))