最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get value not in array - Stack Overflow

programmeradmin3浏览0评论

Given two arrays: (one and two).

one: contains values

two: contains objects with values

I need to get the values from one which are not in two. I've tried with .filter() and .indexOf() but don't get the desired result.

In the following case I expect result to have the value 333. How to achieve that?

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

var result = two.filter(function (item) {
    console.log(item.identifier, one.indexOf(item.identifier));
});

console.log('result: ', result);

Given two arrays: (one and two).

one: contains values

two: contains objects with values

I need to get the values from one which are not in two. I've tried with .filter() and .indexOf() but don't get the desired result.

In the following case I expect result to have the value 333. How to achieve that?

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

var result = two.filter(function (item) {
    console.log(item.identifier, one.indexOf(item.identifier));
});

console.log('result: ', result);

Share Improve this question edited Oct 24, 2019 at 8:34 Yosvel Quintero 19.1k5 gold badges39 silver badges47 bronze badges asked Aug 9, 2017 at 14:21 carambacaramba 22.5k20 gold badges93 silver badges133 bronze badges 2
  • 1 Possible duplicate of Filter Array Not in Another Array – Daniel Beck Commented Aug 9, 2017 at 14:23
  • 1 Or stackoverflow./questions/2963281/… may be better – Daniel Beck Commented Aug 9, 2017 at 14:24
Add a ment  | 

8 Answers 8

Reset to default 2

Just do what you want, filter one and take only those which are not in two (find it in two, if found it will return the object i.e. true equivalent if not found then it will return undefined i.e. false equivalent and negate ! it)

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];
var resultArr = one.filter(function(val){
    return !two.find(function(obj){
        return val===obj.identifier;
    });
});

console.log(resultArr)

I would extract the identifier values from two and then run the filter over one:

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

// get a flattened array of identifier values [111, 222, 444]
const identifiers = two.map((item) => item.identifier);

var result = one.filter(function (item) {
    console.log(item, identifiers.indexOf(item) === -1);
    return identifiers.indexOf(item) === -1;
});

console.log('result: ', result);

You do not return a Boolean value from .filter().

You can iterate one array and can use .some() and ! operator to check if the current value exists at two array "identifier" property

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

var result = one.filter(function (item) {
    return !two.some(function(el) {
      return el.identifier === item
    })
});

console.log('result: ', result);

You can filter the array one returning the elements that are not found in array two.

Code:

const one = [111, 222, 333, 444];
const two = [{identifier: 111},{identifier: 222},{identifier: 444}];
const result = one.filter(oneElem => !two.find(twoElem => oneElem === twoElem.identifier));

console.log('result: ', result);

You could use a Set and filter the values of one.

var one = [111, 222, 333, 444],
    two = [{ identifier: 111 }, { identifier: 222 }, { identifier: 444 } ],
    result = one.filter((s => a => !s.has(a))(new Set(two.map(o => o.identifier))));

console.log(result);

You can map the variable two first

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];
two = two.map(function(item) {
  return item.identifier;
});

var result = one.filter(function (item) {
    return two.indexOf(item) == -1;
});

console.log('result: ', result);

You can create a temporary array with all the values from array two. Then use indexOf to check if any value from array one is missin in array two

var one = [111, 222, 333, 444];
var two = [{
    identifier: 111
  },
  {
    identifier: 222
  },
  {
    identifier: 444
  }
];
var tempArray = []
two.forEach(function(item) {
  tempArray.push(item.identifier)

})
one.forEach(function(item) {
  if (tempArray.indexOf(item) === -1) {
    console.log(item)
  }
})

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

vals = two.map(e=>{ return e['identifier']})

val = one.filter(e => { return vals.indexOf(e) == -1})
console.log(val)

map values into array and then filter the first array.

发布评论

评论列表(0)

  1. 暂无评论