JavaScript 将数组元素移动到数组末尾
JavaScript move array element to end of array
编辑 - 2 个优雅的解决方案
两个优雅的解决方案,来自 Pranav 和 Bekim。谢谢,两者都经过测试并且运行良好。
一个
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
两个
var res = data.slice(),
len = res.length;
for (var i = 0; i < len; i++) {
if (res[i].name == 'other') {
res.push(res.splice(i, 1)[0]);
i--;
len--;
}
}
IM 使用的 JS 工具
Angular 1.5.6,lodash 4.1x
这是我有一个按字母顺序排序的对象数组的场景,例如下面的 sortedData 等。但是,在该数组中也是捕获所有 Other
,它显然也是按字母顺序排序的。我想从数组中删除 other 然后移动到数组的末尾而不弄乱当前排序的数组。
注意
我目前的以下方法有效但很丑陋。 JS、angular 或 lodash 有没有更优雅的东西?
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
function move(array, fromIndex, toIndex) {
array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
return array;
}
var moved = move(
data,
_.findIndex(data, ['name', 'other']),
Object.keys(data).length
);
理想的结果
var data = [
{id:1,name:'one'},
{id:2,name:'two'},
{id:4,name:'four'},
{id:5,name:'five'}
{id:3,name:'other'},
]
使用简单for loop
var data = [{
id: 1,
name: 'apple'
}, {
id: 2,
name: 'banana'
}, {
id: 3,
name: 'other'
}, {
id: 4,
name: 'tomato'
}, {
id: 5,
name: 'strawberry'
}];
// store the length of array
var len = data.length;
// iterate over all array elements
for (var i = 0; i < len; i++) {
// check value is `other`
if (data[i].name == 'other') {
// if value is other then move the object
data.push(data.splice(i, 1)[0]);
// decrement i since the element removed
i--;
// decrement len to avoid moved element
len--;
}
}
console.log(data);
普通(香草)JavaScript 就可以了:
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
console.log( data );
你在纯 Javascript (ES6) 中做 Array.findIndex,没有使用任何库:
data.push(data.splice(data.findIndex(v => v.name == 'other'), 1)[0])
Array.findIndex is new,但现在人们实际使用的大多数浏览器都有它。所以你可能会使用它。 (Edge 支持,但 IE 不支持)。
如果您觉得 [0]
难看,您可以使用 spread 来解压缩数组(这样它也适用于多个项目并进行一些更改):
data.push(...data.splice(data.findIndex(v => v.name == 'other'), 1))
编辑 - 2 个优雅的解决方案
两个优雅的解决方案,来自 Pranav 和 Bekim。谢谢,两者都经过测试并且运行良好。
一个
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
两个
var res = data.slice(),
len = res.length;
for (var i = 0; i < len; i++) {
if (res[i].name == 'other') {
res.push(res.splice(i, 1)[0]);
i--;
len--;
}
}
IM 使用的 JS 工具
Angular 1.5.6,lodash 4.1x
这是我有一个按字母顺序排序的对象数组的场景,例如下面的 sortedData 等。但是,在该数组中也是捕获所有 Other
,它显然也是按字母顺序排序的。我想从数组中删除 other 然后移动到数组的末尾而不弄乱当前排序的数组。
注意
我目前的以下方法有效但很丑陋。 JS、angular 或 lodash 有没有更优雅的东西?
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
function move(array, fromIndex, toIndex) {
array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
return array;
}
var moved = move(
data,
_.findIndex(data, ['name', 'other']),
Object.keys(data).length
);
理想的结果
var data = [
{id:1,name:'one'},
{id:2,name:'two'},
{id:4,name:'four'},
{id:5,name:'five'}
{id:3,name:'other'},
]
使用简单for loop
var data = [{
id: 1,
name: 'apple'
}, {
id: 2,
name: 'banana'
}, {
id: 3,
name: 'other'
}, {
id: 4,
name: 'tomato'
}, {
id: 5,
name: 'strawberry'
}];
// store the length of array
var len = data.length;
// iterate over all array elements
for (var i = 0; i < len; i++) {
// check value is `other`
if (data[i].name == 'other') {
// if value is other then move the object
data.push(data.splice(i, 1)[0]);
// decrement i since the element removed
i--;
// decrement len to avoid moved element
len--;
}
}
console.log(data);
普通(香草)JavaScript 就可以了:
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
console.log( data );
你在纯 Javascript (ES6) 中做 Array.findIndex,没有使用任何库:
data.push(data.splice(data.findIndex(v => v.name == 'other'), 1)[0])
Array.findIndex is new,但现在人们实际使用的大多数浏览器都有它。所以你可能会使用它。 (Edge 支持,但 IE 不支持)。
如果您觉得 [0]
难看,您可以使用 spread 来解压缩数组(这样它也适用于多个项目并进行一些更改):
data.push(...data.splice(data.findIndex(v => v.name == 'other'), 1))