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:{}
个数组。我该如何删除?以及如何找到第一项的价值?
答案 0 :(得分:2)
由于数组的第一个元素始终是索引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;
}
答案 1 :(得分:2)
可以有多种方法。
shift()
let arr = [{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}]
arr.shift();
console.log(arr);
注意:shift()
将修改原始数组。
splice()
let arr = [{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}]
arr.splice(0,1);
console.log(arr);
注意:splice()
将修改原始数组。
slice
let arr = [{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 res = arr.slice(1)
console.log(res);
let arr = [{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}]
const [,...rest] = arr
console.log(rest);
答案 2 :(得分:1)
据我从您的问题中了解到,您有类似下面的内容,必须从Array1中删除Array2,
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个元素。
答案 3 :(得分:0)
//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()的详细链接 删除第一个元素:
答案 4 :(得分:0)
使用以下方法从数组中删除一个数组索引的简单方法
var ar = ['zero', 'one', 'two', 'three'];
ar.shift(); // returns "zero"
console.log( ar );
如果是这样的数组,则可以使用以下命令删除0索引。
var ar = [
{ id: 155382506003, toppings: 500},
{ id: 155382506002, toppings: 100},
{ id: 155382506001, toppings: 200}
];
ar.shift();
console.log( ar );
答案 5 :(得分:0)
您有一个Javascript对象数组。您可以通过以下方式删除第一个元素:
使用shift
函数,例如:
var first = fruits.shift(); // remove Apple from the front
例如,使用splice
函数-通过索引位置删除元素:
var removedItem = fruits.splice(pos, 1); // this is how to remove an item
您可以按索引访问元素的值,例如:
var first = fruits[0];
您可以使用foreach
查找字段的值,例如:
fruits.forEach(function(item, index, array) {
if (item.id === 1553825061863) {
console.log(item, index);
}
});