admin管理员组文章数量:1355697
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
8 Answers
Reset to default 2Just 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.
本文标签: javascriptGet value not in arrayStack Overflow
版权声明:本文标题:javascript - Get value not in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743975238a2570851.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论