0

I might have written a pretty confusing title but my question is rather simple.

I'm looking for an efficient way to remove an item from an array. But my array is full objects that has been stringified (I'm writing my app on Node.js and I'm using JSON.stringify method). So my array is like this;

"{\"userID\":\"15\",
  \"possibleFollowers\":[
      {\"followerID\":\"201\",\"friends\":716},
      {\"followerID\":\"202\",\"friends\":11887},
      {\"followerID\":\"203\",\"friends\":11887}],
  \"name\":\"John\",
  \"lon\":\"Doe\"}"

My question is on Javascript(or Node). If I wanted to remove the from possibleFollowers with "followerID: 202", how would I be able to do that efficiently?

3
  • do you want to remove from JSON string or Javascript object ? Commented Oct 19, 2012 at 7:49
  • @Diode the object from the possible folllowers. I mean if it is "possibleFollowers\":[ {\"followerID\":\"201\",\"friends\":716}, {\"followerID\":\"202\",\"friends\":11887}, {\"followerID\":\"203\",\"friends\":11887}], after the removal it should look like this; Commented Oct 19, 2012 at 7:51
  • "possibleFollowers\":[ {\"followerID\":\"201\",\"friends\":716}, {\"followerID\":\"202\",\"friends\":11887} ] Commented Oct 19, 2012 at 7:51

4 Answers 4

3
var string = "…";

var obj = JSON.parse(string);
obj.possibleFollowers = obj.possibleFollowers.filter(function(fol) {
    return fol.followerID != "202";
});
string = JSON.stringify(obj);
Sign up to request clarification or add additional context in comments.

1 Comment

Beware of IE support though; IE >= 9 .. for node.js it shouldn't matter i guess :)
2
var data = "{\"userID\":\"15\",\"possibleFollowers\":[{\"followerID\":\"201\",\"friends\":716},{\"followerID\":\"202\",\"friends\":11887},{\"followerID\":\"203\",\"friends\":11887}],\"name\":\"John\",\"lon\":\"Doe\"}";

var dataObject = JSON.parse(data);
dataObject.possibleFollowers = dataObject.possibleFollowers.filter(function(follower) {
    return !(follower.followerID == "202");
});
data = JSON.stringify(dataObject);

2 Comments

Thank you so much for the answer. I haven't tried yet but looks just fine. However I was wondering, does your method and the well suggest splice method have a huge difference in terms of efficiency?
I was going to comment on the dangers of modifying an array while looping over it by index, and then the answer changed to use filter, which is what I was going to suggest! +1
0

In javascript, the splice method is used to delete an array element by index.

see :

http://www.roseindia.net/java/javascript-array/javascript-array-remove-index.shtml

2 Comments

Seen, searched but could not adopt to my problem. Yet thanks for the suggestion.
An additionally I would like to search for the object, then delete it.
-1

try just to delete it by using "delete"

for (var i in possibleFollowers) {
    if (possibleFollowers[i]['followerId'] == '216') {
        delete possibleFollowers[i];
    }
}

1 Comment

That will create a null entry in its place.

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.