Solved

json array, remove all elements contained in another json array

Posted on 2015-01-22
2
105 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now