Javascript 通过匹配值组合对象数组

Javascript 通过匹配值组合对象数组,javascript,ajax,Javascript,Ajax,我想组合两个对象数组,以便更容易在HTML中显示。该函数应该在arr1中查找名为“id”的键的匹配值,在arr2中查找名为“source”的键的匹配值。下面是它的样子: let arr1 = [ {id = 1, name = "Anna"}, {id = 2, name = "Chris"} ] let arr2 = [ {childName = "Brian", {source = 1}}, {childName = "Conn

我想组合两个对象数组,以便更容易在HTML中显示。该函数应该在arr1中查找名为“id”的键的匹配值,在arr2中查找名为“source”的键的匹配值。下面是它的样子:

   let arr1 = [
   {id = 1,
   name = "Anna"},

   {id = 2,
   name = "Chris"}
   ]

   let arr2 = [
   {childName = "Brian",
   {source = 1}},
   {childName = "Connie",
   {source = 2}}
   {childName = "Dory",
   {source = 1}}
   ]
我尝试了不同的方法,最好的方法是在数组上使用forEach和filter。我正在尝试在arr1对象中设置一个名为“children”的新属性

arr1.forEach(el=>el.children=arr2.filter(checkMatch));
函数检查匹配(子级){

对于(设i=0;i由于您创建arr1和arr2对象的语法无效,我尝试猜测您的对象的结构

让arr1=[
{
id:1,
姓名:“安娜”
},
{
id:2,
姓名:“克里斯”
}
];
设arr2=[
{
孩子名字:“布莱恩”,
资料来源:1
},
{
孩子名字:“康妮”,
资料来源:2
},
{
孩子的名字:“多莉”,
资料来源:1
}
];
arr2.map((子项)=>{
for(让arr1的父级){
if(parent.id==child.source){
如果(!parent.children){
parent.children=[];
}
父。子。推(子);
}
}
});

console.log(arr1);
您的JSON有问题,但我整理了一下,下面是使用
map
过滤器的选项

const arr1=[{
id:1,
姓名:“安娜”
},
{
id:2,
姓名:“克里斯”
}];
常数arr2=[{
孩子名字:“布莱恩”,
家长:{
资料来源:1
}
},
{
孩子名字:“康妮”,
家长:{
资料来源:2
}
},
{
孩子的名字:“多莉”,
家长:{
资料来源:1
}
}];
让merge=arr1.map(p=>{
p、 children=arr2.filter(c=>c.parent.source==p.id).map(c=>c.childName);
返回p;
});

console.log(merge);
下面使用
Map
存储和方便地查找父级

const parents = [
  {
    id: 1,
    name: "Anna"
  },
  {
    id: 2,
    name: "Chris"
  }
]

const children = [
  {
    childName: "Brian",
    source: 1
  },
  {
    childName: "Connie",
    source: 2
  },
  {
    childName: "Dory",
    source: 1
  }
]

// Create a map for easy lookup of parents.
const parentMap = new Map()

// Add parents to the map, based on their id.
parents.forEach(parent => parentMap.set(parent.id, parent))

// Add children to their parents.
children.forEach((child) => {
  // Get the parent from the map.
  const parent = parentMap.get(child.source)

  // Handle parent not found error.
  if (!parent) { return console.error('parent not found for', child.childName)}

  // Create the children array if it doesn't already exist.
  parent.children = parent.children || []

  // Add the child to the parent's children array.
  parent.children.push(child)
})

// Output the result.
Array.from(parentMap).forEach(parent => console.log(parent[1]))
结果:

{ 
  id: 1,
  name: 'Anna',
  children: [ 
    { childName: 'Brian', source: 1 },
    { childName: 'Dory', source: 1 } 
  ] 
}
{ 
  id: 2,
  name: 'Chris',
  children: [ 
    { childName: 'Connie', source: 2 } 
  ] 
}

您的json有一些您应该使用的问题

:

而不是

=

还有一些大括号使结构不正确,但我认为这里要做的是用主题的childNames填充children子数组这是我的方法:

var json=
[
{
“id”:1,
“姓名”:“安娜”
},
{
“id”:2,
“姓名”:“克里斯”
}
];
var subson=[
{
“childName”:“Brian”,
“来源”:1
},
{
“孩子名”:“康妮”,
“来源”:2
},
{“childName”:“Dory”,
“来源”:1
}
];
var newJson=[];
$.each(json,函数(index1,项){
push({“id”:item.id,“name”:item.name,“children”:[]});
$。每个(主题,功能(索引2,子项){
if(subitem.source==item.id){
newJson[newJson.length-1].children.push({“childName”:subitem.childName});
}
})
})
console.log(newJson);

这不是对象的正确语法。它应该是
id:1
,而不是
id=1
。在
arr2
中,
source=1
周围不应该有另一组
{}
{ 
  id: 1,
  name: 'Anna',
  children: [ 
    { childName: 'Brian', source: 1 },
    { childName: 'Dory', source: 1 } 
  ] 
}
{ 
  id: 2,
  name: 'Chris',
  children: [ 
    { childName: 'Connie', source: 2 } 
  ] 
}