Solved

json array, remove all elements contained in another json array

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

807 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