3
let selected = [
  {id: 15, name: 'Canada'},
  {id: 25, name: 'Germany'}
];

let all = [
  {id: 15, name: 'Canada'},
  {id: 25, name: 'Germany'},
  {id: 32, name: 'United States'},
  {id: 40, name: 'China'}
]

How do I get non-selected countries from all objects and print it out in another variable? Based on id key of those which are in selected array?

1
  • 1
    Caveat, remember that {id: 25, name: 'Germany'} !== {id: 25, name: 'Germany'}. Commented Feb 3, 2017 at 16:53

3 Answers 3

5

You need to find all objects that aren't contained in selected and then do something with them:

let nonSelectedItems = all.filter(obj => selected.every(s => s.id !== obj.id));

//do stuff with non-selected items
Sign up to request clarification or add additional context in comments.

4 Comments

oops I forgot to say that it's React's object. typeof returns object :(
@knitevision: What do you mean by "React's object"? What does that have to do with the problem?
@FelixKling Array.prototype.filter does not work on objects :(
@knitevision: So you are saying, all is not an array like in your example? Why did you make the question about arrays then? We cannot help you if you are asking about something different than you have. Please update your question and provide a better example. Btw, typeof returns "object" for arrays because arrays are objects.
3

You can use filter and find, so as soon as element with same id is found in selected it will filter out that element from all. You can also use some instead of find.

let selected = [
  {id: 15, name: 'Canada'},
  {id: 25, name: 'Germany'}
];

let all = [
  {id: 15, name: 'Canada'},
  {id: 25, name: 'Germany'},
  {id: 32, name: 'United States'},
  {id: 40, name: 'China'}
]

var r = all.filter(e => !selected.find(a => e.id === a.id));
console.log(r)

Comments

2

Generate an object which holds id as a property using Array#reduce method(which helps to speed up since you need to iterate over and over) and use Array#filter method to filter elements from all array.

// generate the object reference
let ref = selected.reduce(function(obj, o) {
  // define property
  obj[o.id] = true;
  // return object property
  return obj;
  // set initial value as an object
}, {});

// filter out array elements
let res = all.filter(function(o) {
  return !ref[o.id]
})

let selected = [{
  id: 15,
  name: 'Canada'
}, {
  id: 25,
  name: 'Germany'
}];

let all = [{
  id: 15,
  name: 'Canada'
}, {
  id: 25,
  name: 'Germany'
}, {
  id: 32,
  name: 'United States'
}, {
  id: 40,
  name: 'China'
}]

let ref = selected.reduce(function(obj, o) {
  obj[o.id] = true;
  return obj;
}, {});

console.log(
  all.filter(function(o) {
    return !ref[o.id]
  })
)


With ES6 arrow function :

let ref = selected.reduce((obj, o) => (obj[o.id] = true, obj), {});

let res = all.filter(o => !ref[o.id]);

let selected = [{
  id: 15,
  name: 'Canada'
}, {
  id: 25,
  name: 'Germany'
}];

let all = [{
  id: 15,
  name: 'Canada'
}, {
  id: 25,
  name: 'Germany'
}, {
  id: 32,
  name: 'United States'
}, {
  id: 40,
  name: 'China'
}]

let ref = selected.reduce((obj, o) => (obj[o.id] = true, obj), {});

console.log(
  all.filter(o => !ref[o.id])
)

Comments

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.