0

I have an array of json objects, for eg

 var user =[ { id: 1, name: 'linto', img: 'img' },
  { id: 2, name: 'shinto', img: 'img' },
  { id: 3, name: 'hany', img: 'img' } ]

From this, i need to remove a particular json object

{ id: 2, name: 'shinto', img: 'img' }.

ie the output array should be like this

[ { id: 1, name: 'linto', img: 'img' },
  { id: 3, name: 'hany', img: 'img' } ]

Is there any function in node.js to achieve this ?

1

2 Answers 2

0

use delete command for that.

E.g.

delete user[index]

EDIT: splice command works better in case of array as it fully removes the element. In case of delete null replaces the deleted element.

user.splice(0,<index>);

Full code :

var user =[ { id: 1, name: 'linto', img: 'img' },
  { id: 2, name: 'shinto', img: 'img' },
  { id: 3, name: 'hany', img: 'img' } ]

  alert(JSON.stringify(user));

  user.splice(0,1);

  alert(JSON.stringify(user));
Sign up to request clarification or add additional context in comments.

4 Comments

index on what basis ?
i know, if we loop , it will ok.B ut is there any predefined function for that ?
I think in underscore.js some function is there to find index , this post might help you for that stackoverflow.com/questions/21522319/…
@Sachin.That whats i exactly want.Thanks
0

There is an underscore way for the same.

reject

var user = [{
  id: 1,
  name: 'linto',
  img: 'img'
}, {
  id: 2,
  name: 'shinto',
  img: 'img'
}, {
  id: 3,
  name: 'hany',
  img: 'img'
}]
console.log(_.reject(user, function(usr) {
  return usr.name == 'shinto';
}));
<script src="http://underscorejs.org/underscore.js"></script>

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.