将数组中的项目移动到最后一个位置

我有一个对象数组。我要将选定的对象移动到数组中的最后一个位置。如何在 javascript 或 jquery 中实现这一点?

下面是我的一些代码:

var sortedProductRow = this.product_row;


for (var s in sortedProductRow) {
if (sortedProductRow[s]["parent_product_type"] != "")
// Move this object to last position in the array
}

我通过一个 for 循环来遍历这个过程,我希望输出是有序的,这样所有没有“ father _ product _ type”值的对象都会优先排序,然后是那些有值的对象。

84626 次浏览

要将一个元素(其中的索引)移动到数组的末尾,请执行以下操作:

array.push(array.splice(index, 1)[0]);

如果没有索引,只有元素,那么这样做:

array.push(array.splice(array.indexOf(element), 1)[0]);

例如:

    var arr = [1, 2, 6, 3, 4, 5];
arr.push(arr.splice(arr.indexOf(6), 1)[0]);
console.log(arr); // [1, 2, 3, 4, 5, 6]

注意:

这只适用于 Ararray (使用 [ ... ]语法或 (使用 { ... }语法或 Object())

移动同一数组的数组 直到最后第一个元素

    var a = [5,1,2,3,4];
a.push(a.shift());
console.log(a); // [1,2,3,4,5]

或者这边

    var a = [5,1,2,3,4];
var b = a.shift();
a[a.length] = b;
console.log(a); // [1,2,3,4,5]

在同一数组中移动数组 任何位置任何元素

    // move element '5' (index = 2) to the end (index = 4)
var a = [1, 2, 5, 4, 3];
a.splice(4,0,a.splice(2,1)[0]);
console.log(a); // [1, 2, 4, 3, 5]

或者它也可以转换成一个原型,就像这样,x表示元素的当前位置,而 y表示数组中的新位置

var a = [1, 2, 5, 4, 3];
Array.prototype.move = function(x, y){
this.splice(y, 0, this.splice(x, 1)[0]);
return this;
};
    

a.move(2,4);
console.log(a); // ["1", "2", "4", "3", "5"]

回应@jkalandarov 的评论

function moveToTheEnd(arr, word){
arr.map((elem, index) => {
if(elem.toLowerCase() === word.toLowerCase()){
arr.splice(index, 1);
arr.push(elem);
}
})
return arr;
}
console.log(moveToTheEnd(["Banana", "Orange", "Apple", "Mango", "Lemon"],"Orange"));

使用匿名函数可以传递数组和要筛选的值。

let concatToEnd = function (arr, val) {
return arr.filter(function(x) {
return x !== val; // filter items not equal to value
}).concat(arr.filter(function(x) { // concatonate to filtered array
return x === val; // filter items equal to value
})
);
}


// invoke
concatToEnd(array, 'parent_product_type');

你也许可以进一步缩短:

let concatToEnd = (arr,val) => arr.filter(x => x !== val).concat(arr.filter(x => x === val))

这个函数过滤传入值 不等于的条目,然后连接 另一个 filter函数的结果(到过滤数组的末尾) ,该函数过滤掉传入值 做同样的事的条目。

这个函数实际上将数组分成两个经过过滤的部分,然后将它们连接在一起

这还没有在您的用例中进行测试,但是我已经使用了类似的方法来将数组的所有数字移动到索引的末尾。

将任何元素移动到最后一个位置-对于 洛达什用户:

    const array = ['A', 'B', 'C', 'D'] // output: A, B, C, D


// finds index of value 'B' and removes it
_.pull(array , 'B') // output: A, C, D


// adds value of 'B' to last position
_.concat(array , 'B') // output: A, C, D, B

这样更干净,不使用[0]的数组索引

const colors = ['white', 'black', 'red', 'blue', 'green'];


// will push the blue to the end of the array
colors.push(colors.splice(colors.indexOf('blue'), 1).pop());


console.debug(colors);
// ["white", "black", "red", "green", "blue"]

你可以移动任意数量的项目通过 拼接他们,然后 扩散他们成为一个 用力

const numberOfItems = 3;
let items = [1,2,3,4,5,6,7,8,9];
items.push(...items.splice(0, itemsToMove))

不变的方式:

const changedArr = [...prevArr.filter(a => a !== element), element]

移动所有的项目等于这样的东西,以结束也可以做连接。

const myArray = [1, 0, 3, 5, 0, 'a', 3, 's']
    

const moveZerosToEnd = (arr) => {
return arr.filter(item => item !== 0).concat(arr.filter(item => item === 0))
}


console.log(myArray)                 // [1, 0, 3, 5, 0, 'a', 3, 's']
console.log(moveZerosToEnd(myArray)) // [1, 3, 5, 'a', 3, 's', 0, 0]