array remove java_how to remove array from another array in javascript

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

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, …}

I want to remove 0:{} array in array. how can i remove? and how to find the value of the first item?

回答1:

Since an array's first element is always index 0, you can use Array.prototype.shift which removes the first element:

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;

}

回答2:

There can be multiple ways of doing that.

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);

Note: shift() will modify the original array.

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);

Note: splice() will modify the original array.

Using 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);

Using Spread Operator And Destructuring Assignment

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);

回答3:

//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"}

Here is the detailed link about Array.protoType.shift() which removes the first element:

回答4:

the simple way to remove one array index form array using

var ar = ['zero', 'one', 'two', 'three'];

ar.shift(); // returns "zero"

console.log( ar );

if array like this you can delete 0 index using the following command.

var ar = [

{ id: 155382506003, toppings: 500},

{ id: 155382506002, toppings: 100},

{ id: 155382506001, toppings: 200}

];

ar.shift();

console.log( ar );

回答5:

You have an array of Javascript objects. You can remove the first element by:

using shift function, for example:

var first = fruits.shift(); // remove Apple from the front

using splice function, for example - remove an element by index position:

var removedItem = fruits.splice(pos, 1); // this is how to remove an item

you can access value of an element by index, for example:

var first = fruits[0];

you can find value of a field by using foreach, for example:

fruits.forEach(function(item, index, array) {

if (item.id === 1553825061863) {

console.log(item, index);

}

});

回答6:

As I understood from your question that you have something like below that you have to remove Array2 from 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, …}

If so just try as below using the filter function.

var data = Array1;

var selectedRows = Array2;

var unSelectedRows = [];

var unSelectedRows = data.filter( function( el ) {

return !selectedRows.includes( el );

} );

You can get the 1 st and 2nd element in unSelectedRows Array.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值