0
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?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Aung Htet Paing
  • 121
  • 1
  • 1
  • 9
  • 3
    Possible duplicate of [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – Eddie Mar 29 '19 at 03:05
  • If you want to remove first element, you can use [Array.shift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) – Shidersz Mar 29 '19 at 03:06
  • Please elaborate a little more. What is your expected output. If you just want to remove the first use `Array.shift()` or `Array.slice(1)` – Maheer Ali Mar 29 '19 at 03:07
  • first -> find array element second -> delete array element. how can i do – Aung Htet Paing Mar 29 '19 at 03:09
  • Can you please post the actual code for your data structure? From what you've posted here it's not clear whether you're dealing with arrays, or with an object with numeric keys. (Given that you're also describing the internal objects as "arrays" I suspect the external container is also actually an object...) – Daniel Beck Mar 29 '19 at 03:16

6 Answers6

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;
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1
//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:

Harshit Pant
  • 1,032
  • 11
  • 14
1

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);
Community
  • 1
  • 1
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

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.

Hariprasath
  • 539
  • 1
  • 9
  • 21
0

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 );
nirali
  • 73
  • 5
0

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);
    }
});
Linh Dao
  • 1,305
  • 1
  • 19
  • 31