1

I have an array of objects and want to remove one object from this array:

var objectArray = [{"id":"John Doe","label":"John Doe","shape":"image","image":"app/data/img/user_icon.png","color":{"background":"#db630d","border":"#7c3400"},"level":0},{"id":"JavaScript","label":"JavaScript","size":30,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"C#","label":"C#","size":40,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"css","label":"css","size":40,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"develop","label":"develop","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2},{"id":"programming","label":"programming","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2},{"id":"frontend","label":"frontend","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2}]

var singleNode = {"id":"frontend","label":"frontend","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2}

i want to remove this object from object array mentioned above.. how can i do that using angularjs or javascript?

2

3 Answers 3

3

You can use array filter if id of each object is unique.

Considering id as unique

var objectArray = [{"id":"John Doe","label":"John Doe","shape":"image","image":"app/data/img/user_icon.png","color":{"background":"#db630d","border":"#7c3400"},"level":0},{"id":"JavaScript","label":"JavaScript","size":30,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"C#","label":"C#","size":40,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"css","label":"css","size":40,"shape":"dot","color":{"background":"#00637c"},"level":1},{"id":"develop","label":"develop","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2},{"id":"programming","label":"programming","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2},{"id":"frontend","label":"frontend","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2}]

var singleNode = {"id":"frontend","label":"frontend","size":40,"shape":"box","color":{"background":"#65f759","border":"#65f759"},"level":2}

var filteredArray = objectArray.filter(function(item){
  return item.id !== "frontend";
});

console.log(filteredArray)

Sign up to request clarification or add additional context in comments.

Comments

0
let index = objectArray.findIndex(object => object.id === singleNode.id);
if (index != -1) {
    objectArray.splice(index, 1);
}

Comments

0

First, find the index of the element you want to remove:

var array = [2, 5, 9];
var index = array.indexOf(5);

Then remove it with splice:

if (index > -1) {
    array.splice(index, 1);
}

Comments

Your Answer

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.