Solved

json array, remove all elements contained in another json array

Posted on 2015-01-22
2
113 Views
Last Modified: 2015-01-25
Hello Experts,

I've seen several examples on the web filtering arrays, using for example jQuery grep.
But all i see i a little more simple than what i want.

Maybe this is more simple that i think..

I have this two arrays:
var searchtag_catg       = [{ id: 2, tb: 'text2', search: 'test2'},{ id: 4, tb: 'text4', search: 'test4'}];

var categories       = [{ id: 1, tb: 'text1', search: 'test1'},{ id: 2, tb: 'text2', search: 'test2'},{ id: 3, tb: 'text3', search: 'test3'},{ id: 4, tb: 'text4', search: 'test4'}];

Open in new window

And i want to show the records in the array categories that are not in searchtag_catg array, using the id key...
How can i do this?

Thx in advanced :)
Miguel
0
Comment
Question by:justaphase
2 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
ID: 40565905
Something like this:
var searchtag_id = searchtag_catg.map( function(v,i){ return v.id; } );
var good_categories = categories.filter( function(v,i) { return (searchtag_id.indexOf(v.id) < 0); } );

Open in new window

0
 
LVL 1

Author Closing Comment

by:justaphase
ID: 40569220
Sorry i didn't answer sooner!

Steve, amazing!

Worked like a charm.

Thx :)

Miguel
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question