0

I'm initializing an array with dynamic objects and result is following

var arr = [
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0}
];

now i want to remove any object from this array but not finding proper solution. Can someone please help me how to do it.

Federico klez Culloca
27.3k17 gold badges61 silver badges102 bronze badges
asked Sep 28, 2017 at 7:24
11
  • This will remove any.arr=[]. Commented Sep 28, 2017 at 7:25
  • You need to find index of object, then use splice method to remove the object from array. Commented Sep 28, 2017 at 7:25
  • @Alexandru arr = [] will empty whole array, that's what I don't want. Commented Sep 28, 2017 at 7:28
  • @KashifHussain, You don't specify which objects you want. Commented Sep 28, 2017 at 7:29
  • @VijayRaheja initially when there are no values set in any object as you can see so I can't find index. Array will be exactly what I have shown above and at this stage I have to remove any single object from inside array. If you have solution for that please help Commented Sep 28, 2017 at 7:31

4 Answers 4

1

i want to remove any object from this array but not finding proper solution

You can use splice method, It accepts the index(start) of the to be removed element and also the deleteCount that is number of elements to be removed from the starting index

var arr = [{
 id: "1",
 name: "A",
 quantity: 1
 },
 {
 id: "2",
 name: "B",
 quantity: 2
 },
 {
 id: "3",
 name: "C",
 quantity: 3
 },
 {
 id: "4",
 name: "D",
 quantity: 4
 }
];
arr.splice(1, 1)
console.log(arr)

answered Sep 28, 2017 at 7:32
Sign up to request clarification or add additional context in comments.

Comments

1

at the beginning you must know which object you want to delete it (for example the object whose id = x)

then you can use this code

arr = arr.filter(function(e){ return e.id != x });
answered Sep 28, 2017 at 7:33

Comments

1

Guys thank you so much for your help. I have found solution to this issue, following is what I wanted.

this is my array with initial values set.

var arr = [
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0}
];

and I I can remove any object like this:

arr.splice(arr[index], 1);
answered Sep 28, 2017 at 8:12

Comments

0

The solution is that filter your arr Array which has non-empty values like this-:

 var arr = [
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0},
 {id: "", name: "", quantity: 0}
];
 arr = arr.filter((obj)=>obj.id!==''); // you can use any field to filter 
 arr = arr.filter((obj)=>obj.id!=='' || obj.name!=='');// multiple fields to filter

Now your arr variable will be updated with new array of objects with non-empty fields

answered Sep 28, 2017 at 7:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.