1

I have a sharepoint column named AllLinks in which im storing array as:

[{"AllLinks":"Link9","LinkURL":"http://www.Link9.com"},   
 {"AllLinks":"Link6","LinkURL":"http://www.Link6.com"}]

How to check if a value exists in an array of objects and if a match is found, delete the key value pair.

For example if the value Link6 matches, delete the entry completely from the array using javascript/jquery. I tried with:

var newA = data.d.results.filter(function (item) return item.AllLinks !== x;});  

but item.AllLinks again returns the complete array itself

as AllLinks is a column in my sharepoint list.

3
  • you want to remove it before or without json decode in js ?? Commented Aug 17, 2017 at 10:32
  • 1
    Note that nothing about this is JSON. I changed the title, description and tags for you. Commented Aug 17, 2017 at 10:35
  • But in many json validators it comes out as valid json.Is it really not json and just array of objects? Commented Aug 17, 2017 at 10:48

2 Answers 2

6

You can use filter function:

var a = [{"AllLinks":"Link9","LinkURL":"http://www.Link9.com"},{"AllLinks":"Link6","LinkURL":"http://www.Link6.com"}]

var newA = a.filter(function (item) {
    return item.AllLinks !== "Link6";
});
Sign up to request clarification or add additional context in comments.

Comments

5

You can do this in such an easy way here:

var jsonArry = [{"AllLinks":"Link9","LinkURL":"http://www.Link9.com"},{"AllLinks":"Link6","LinkURL":"http://www.Link6.com"}];

var value = "Link6";

for(var i=0; i<jsonArry.length; i++){
   if(jsonArry[i].AllLinks === value){
        jsonArry.splice(i,1);
   }
}

console.log(JSON.stringify(jsonArry));

If you are sure that the value key is unique then add a break keyword inside the for loop within if after you delete the object so as to prevent unnecessary loops like this,

for(var i=0; i<jsonArry.length; i++){
   if(jsonArry[i].AllLinks === value){
       jsonArry.splice(i,1);
       break;
   }
}

2 Comments

Can't you see that the array item isn't really removed? You still have null in there, which is not what the OP wants.
@PeterMader you are right. Thanks for improving. We can use splice in this case.

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.