1

I have got two arrays in my Jquery

     var results = ["1","2","3","4","5","6"];
     var chosen = ["1","A","3","B","5","C"];

How can i delete everything from results that exists in chosen?

0

3 Answers 3

1
 for(var i = 0; i < chosen.length; i++){
     if(results.indexOf(chosen[i]) > -1){
          results.splice(results.indexOf(chosen[i]),1);
      }
}
console.log(results);

results = > 2,4,6

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

Comments

0

Try,

var results = ["1","2","3","4","5","6"];
var chosen = ["1","A","3","B","5","C"];

var cnt = chosen.length;
while(--cnt, cnt >= 0) {
 if(results.indexOf(chosen[cnt]) > -1){
   chosen.splice(cnt,1)
 }
}

Delete everything from second array that exists in first array Jquery

so console.log(chosen); // "A","B","C" will be the result.

1 Comment

@MuradEmi Glad to help! :) Try to press tick mark present inside anyone of the answers here, that helped you solving your issue. But this is not a compulsion. :)
0

do a foreach on results and check if the value is contained into the chosen array, if it is, remove the value

2 Comments

Actually i did it like this $.each( results, function( key, value ) { if(jQuery.inArray(value, chosen) !== -1){ jQuery.grep(results, function(values) { return value !=values ; }); } });
Why would you use functions for a thing that you can do with a for? And of top of that, they're not even native javascript functions. I think both answers are better than using jquery each and grep, and faster.

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.