admin管理员组文章数量:1400007
I am trying to pare two arrays of objects as following :
const obj1 = [
{name:"Mark"},
{name:"David"},
{name:"Ellie"},
{name:"Zuank"},
{name:"Philip"},
{name:"Den"},
]
const obj2 = [
{name:"Mark"},
{name:"David"},
{name:"Zuank"},
{name:"Philip"},
]
I want to check if the name of every object exists in the second object or not. If yes, I want to push that object to a new array with a new property named "Matched" added to each object with a value of "true". Otherwise, the Matched property will be false. This is the final result I want to achieve :
const res = [
{ name: "Mark", matched: true },
{ name: "David", matched: true },
{ name: "Ellie", matched: false },
{ name: "Zuank", matched: true },
{ name: "Philip", matched: true },
{ name: "Den", matched: false },
]
--Edit Here is what I have tried so far guys :
obj1.map((element, index) => {
if (obj2[index].name === element.name) {
resArr.push({ name: element.name, matched: true })
}
else {
resArr.push({ name: element.name, matched: false })
}
})
I am trying to pare two arrays of objects as following :
const obj1 = [
{name:"Mark"},
{name:"David"},
{name:"Ellie"},
{name:"Zuank"},
{name:"Philip"},
{name:"Den"},
]
const obj2 = [
{name:"Mark"},
{name:"David"},
{name:"Zuank"},
{name:"Philip"},
]
I want to check if the name of every object exists in the second object or not. If yes, I want to push that object to a new array with a new property named "Matched" added to each object with a value of "true". Otherwise, the Matched property will be false. This is the final result I want to achieve :
const res = [
{ name: "Mark", matched: true },
{ name: "David", matched: true },
{ name: "Ellie", matched: false },
{ name: "Zuank", matched: true },
{ name: "Philip", matched: true },
{ name: "Den", matched: false },
]
--Edit Here is what I have tried so far guys :
obj1.map((element, index) => {
if (obj2[index].name === element.name) {
resArr.push({ name: element.name, matched: true })
}
else {
resArr.push({ name: element.name, matched: false })
}
})
Share
Improve this question
edited Aug 27, 2020 at 14:30
Saif
asked Aug 27, 2020 at 14:24
SaifSaif
2851 gold badge6 silver badges26 bronze badges
4
- Can you show what you have tried so far to solve this problem yourself? – Yousaf Commented Aug 27, 2020 at 14:26
- can you please share what have you tried? – Shail_bee Commented Aug 27, 2020 at 14:26
- obj1.map((element, index) => { if (obj2[index].name === element.name) { resArr.push({ name: element.name, matched: true }) } else { resArr.push({ name: element.name, matched: false }) } }) – Saif Commented Aug 27, 2020 at 14:30
- @CodeReady Done. – Saif Commented Aug 27, 2020 at 14:31
6 Answers
Reset to default 2You could do a map
obj1
with some
on obj2 to check if obj1
element is on obj2
const obj1 = [
{ name: "Mark" },
{ name: "David" },
{ name: "Ellie" },
{ name: "Zuank" },
{ name: "Philip" },
{ name: "Den" },
]
const obj2 = [
{ name: "Mark" },
{ name: "David" },
{ name: "Zuank" },
{ name: "Philip" },
]
const res = obj1.map((el1) => ({
name: el1.name,
match: obj2.some((el2) => el2.name === el1.name),
}))
console.log(res)
You can use the function map
to build the desired output and the function some
to check for a specific object by the property name
.
const obj1 = [{name:"Mark"}, {name:"David"}, {name:"Ellie"}, {name:"Zuank"}, {name:"Philip"}, {name:"Den"}, ],
obj2 = [{name:"Mark"}, {name:"David"}, {name:"Zuank"}, {name:"Philip"}, ],
result = obj1.map(({name}) => ({name, matched: obj2.some(({name: name2}) => name2 === name)}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could build a set of obj2:
const names = new Set(obj2.map(it => it.name));
Then mapping obj1 can be done in O(n):
const result = obj1.map(({ name }) => ({ name, matched: names.has(name) }));
You can make use of Array.map and Array.some.
const obj1 = [{name:'Mark'},{name:'David'},{name:'Ellie'},{name:'Zuank'},{name:'Philip'},{name:'Den'}];
const obj2 = [{name:'Mark'},{name:'David'},{name:'Zuank'},{name:'Philip'}];
const getProcessedResult = (data1, data2) => {
return data1.map(d1 => ({
...d1,
"matched": data2.some(d2 => d2.name === d1.name)
}));
}
console.log(getProcessedResult(obj1, obj2))
.as-console-wrapper {
max-height: 100% !important;
}
You could take a Set
for all names and map the new property by checking the set.
const
array1 = [{ name: "Mark" }, { name: "David" }, { name: "Ellie" }, { name: "Zuank" }, { name: "Philip" }, { name: "Den" }],
array2 = [{ name: "Mark" }, { name: "David" }, { name: "Zuank" }, { name: "Philip" }],
names = new Set(array2.map(({ name }) => name)),
result = array1.map(o => ({ ...o, matched: names.has(o.name) }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Just map through obj1 and while mapping filter obj2 to check if the current name is in obj2. If it is then push the name along with the matched value of true into obj3. If it isn't then push the name along with the matched value of false into obj3.
const obj1 = [
{name:"Mark"},
{name:"David"},
{name:"Ellie"},
{name:"Zuank"},
{name:"Philip"},
{name:"Den"}
]
const obj2 = [
{name:"Mark"},
{name:"David"},
{name:"Zuank"},
{name:"Philip"}
]
let obj3 = []
obj1.map(function(a) {
let matched = obj2.filter(b => a.name === b.name);
if (matched.length) {
obj3.push({name: a.name, matched: true});
} else {
obj3.push({name: a.name, matched: false});
}
})
console.log(obj3);
本文标签: Comparing two arrays and filtering one array upon this in javascript and reactStack Overflow
版权声明:本文标题:Comparing two arrays and filtering one array upon this in javascript and react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744104874a2591019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论