Aung Htet Paing
Aung Htet Paing

Reputation: 131

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?

Upvotes: 0

Views: 147

Answers (6)

Hariprasath
Hariprasath

Reputation: 539

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.

Upvotes: 1

Linh Dao
Linh Dao

Reputation: 1663

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

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36564

There can be multiple ways of doing that.

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

Note: shift() will modify the original array.

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

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

Upvotes: 1

nirali
nirali

Reputation: 73

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

Upvotes: 0

Harshit Pant
Harshit Pant

Reputation: 1065

//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:

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44087

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

Upvotes: 1

Related Questions