0: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}
1: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}
2: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}我想删除数组中的0:{}数组。我怎么才能移除?以及如何找到第一项的价值?
发布于 2019-03-29 03:59:57
我从您的问题中了解到,您必须从Array2中删除Array1,如下所示,
Array1 = 0: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}
1: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}
2: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}
Array2 = 0: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …}如果是这样,只需使用过滤器函数,如下所示。
var data = Array1;
var selectedRows = Array2;
var unSelectedRows = [];
var unSelectedRows = data.filter( function( el ) {
return !selectedRows.includes( el );
} );你可以在unSelectedRows阵列中得到第1和第2元素.
发布于 2019-03-29 03:11:16
由于数组的第一个元素总是索引0,所以可以使用Array.prototype.shift删除第一个元素:
const array = [{
id: 1553825061863,
name: "Thai Milk Tea",
qty: "1",
total_amount: 9500,
toppings: 500
}, {
id: 1553825061863,
name: "Thai Milk Tea",
qty: "1",
total_amount: 9500,
toppings: 500
}, {
id: 1553825061863,
name: "Thai Milk Tea",
qty: "1",
total_amount: 9500,
toppings: 500
}];
let remainingArray = array;
remainingArray.shift();
console.log(remainingArray);.as-console-wrapper {
max-height: 100% !important;
top: auto;
}
发布于 2019-03-29 03:16:52
//try this on your console. You can use the shift operator to shift the first element.
//also to remove the last element use pop
>>var myArr = [{id : 1, name: "A"}, {id: 2, name: "B"}, {id:3, name: "C"}];
undefined
>>myArr.shift(0);
{id: 1, name: "A"}
>>myArr
0: {id: 2, name: "B"}
1: {id: 3, name: "C"}下面是关于Array.protoType.shift()的详细链接,它删除了第一个元素:
https://stackoverflow.com/questions/55409898
复制相似问题