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

javascript - Map over two arrays of objects, match properties and store specific info in a new array - Stack Overflow

programmeradmin0浏览0评论

New to javascript and trying to learn! I am trying to map through two array of objects, and if a certain property matches, pull in specific information into an array.

let result;

let arrNames = [{
  name: "A"
}, {
  name: "B"
}, {
  name: "C"
}]

let arrInfo = [{
  name: "A",
  info: "AAA"
}, {
  name: "B",
  info: "BBB"
}, {
  name: "C",
  info: "ccc"
}]

If arrNames.name == arrInfo.name, I would like result to equal arrInfo.info.

What I've tried:

arrNames.map(x => {
if(arrNames.name == arrInfo.name){
   result=arrInfo.info
}

^ This obviously doesn't work -- but I'm wondering if Assign or Filter would be appropriate.

Thanks in advance for your help (apologies that this is probably a dupe)!

New to javascript and trying to learn! I am trying to map through two array of objects, and if a certain property matches, pull in specific information into an array.

let result;

let arrNames = [{
  name: "A"
}, {
  name: "B"
}, {
  name: "C"
}]

let arrInfo = [{
  name: "A",
  info: "AAA"
}, {
  name: "B",
  info: "BBB"
}, {
  name: "C",
  info: "ccc"
}]

If arrNames.name == arrInfo.name, I would like result to equal arrInfo.info.

What I've tried:

arrNames.map(x => {
if(arrNames.name == arrInfo.name){
   result=arrInfo.info
}

^ This obviously doesn't work -- but I'm wondering if Assign or Filter would be appropriate.

Thanks in advance for your help (apologies that this is probably a dupe)!

Share Improve this question edited Oct 12, 2019 at 14:46 Nikhil 6,64110 gold badges35 silver badges72 bronze badges asked Oct 11, 2019 at 19:33 BWeb303BWeb303 3031 gold badge7 silver badges18 bronze badges 1
  • Don't modify the question when existing answers address your original question. Ask a new question instead (I see that you already did). Rolling back to previous version. – Nikhil Commented Oct 12, 2019 at 14:48
Add a ment  | 

3 Answers 3

Reset to default 4

You can use find() inside map() to find the element. This finds the element even if they aren't at the same index in both arrays.

Then use filter() to filter any undefined values which will be present if there isn't a match.

var arrNames = [
   {name: "A"},
   {name: "B"},
   {name: "C"}
];

var arrInfo = [
   {name: "A", info: "AAA"},
   {name: "B", info: "BBB"},
   {name: "C", info: "ccc"}
];

let result = arrNames.map(x => {
  item = arrInfo.find(item => item.name === x.name);
  if (item) { 
    return item.info;
  }      
}).filter(item => item !== undefined); // Can also use filter(item => item);

console.log(result);

let result = [];

arrNames = [
   {name: "A"},
   {name: "B"},
   {name: "C"},
]

arrInfo = [
   {name: "A", info: "AAA"},
   {name: "B", info: "BBB"},
   {name: "C", info: "ccc"},
]

result = arrNames.map(function(_, index){
  if(arrNames[index].name === arrInfo[index].name) {
    return arrInfo[index].info
  }
})

You can use map with a condition inside

el.name === arrNames[index].name && el.info

Means:

if (el.name === arrNames[index].name) return el.info

let arrNames = [{
  name: "A"
}, {
  name: "B"
}, {
  name: "C"
}]

let arrInfo = [{
  name: "A",
  info: "AAA"
}, {
  name: "B",
  info: "BBB"
}, {
  name: "C",
  info: "ccc"
}]


const res = arrInfo.map((el, index) => el.name === arrNames[index].name && el.info)

console.log(res)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论