Solved

json array, remove all elements contained in another json array

Posted on 2015-01-22
2
119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)

730 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