Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用键、值对将两个不同大小的对象数组合并到一个数组中?_Javascript_Angular_Typescript_Lodash - Fatal编程技术网

Javascript 如何使用键、值对将两个不同大小的对象数组合并到一个数组中?

Javascript 如何使用键、值对将两个不同大小的对象数组合并到一个数组中?,javascript,angular,typescript,lodash,Javascript,Angular,Typescript,Lodash,我有两个JSON格式的对象数组: { "Arr1": [ { "_id": "firstSub1", "count": 1, "price": 4 }, { "_id": "firstSub2", "count": 2, "price": 7 }, { "_id": "firstSub3", "count": 3, "price": 1 } { "_id": "firstSub4", "count": 4, "price": 1 } ],

我有两个JSON格式的对象数组:

   {
  "Arr1":
   [
    { "_id": "firstSub1", "count": 1, "price": 4 },
    { "_id": "firstSub2", "count": 2, "price": 7 },
    { "_id": "firstSub3", "count": 3, "price": 1 }
    { "_id": "firstSub4", "count": 4, "price": 1 }
   ],

    "Arr2":
     [
      { "name": "firstSub1", "date": 05 / 20 / 1998, "type": sometype1 },
      { "name": "firstSub2"  "date": 12 / 22 / 2011, "type": sometype2 },
      { "name": "firstSub3", "date": 09 / 23 / 2004, "type": sometype3 }
      { "name": "firstSub9", "date": 09 / 23 / 2004, "type": sometype9 }
    ]
//Desired Output
    "finalArray":
     [
      { "name": "firstSub1", "date": 05 / 20 / 1998, "type": sometype1, "count": 1, "price": 4 },  
      { "name": "firstSub2"  "date": 12 / 22 / 2011, "type": sometype2, "count": 2, "price": 7 },
      { "name": "firstSub3", "date": 09 / 23 / 2004, "type": sometype3, "count": 3, "price": 1 },
      { "name": "firstSub9", "date": 09 / 23 / 2004, "type": sometype9 },
      { "_id": "firstSub4", "count": 4, "price": 1 }
          ]   

}
我需要比较第一个数组中的
\u id
,看看Arr2中是否有与
name
匹配的内容,如果
\u id==name
则匹配它们

我尝试过使用lodash及其UndeScore,以及以下映射函数:

  mergeArray() {
    .... //pulling data

    let Arr1 = data['Arr1Data'];
    let Arr2 = data['Arr2Data'];

    let finalArray = Arr2.map((e, _) =>
      (_ = Arr1.find((q) => e.name === q._id)) ?
        { ...e, ..._ } : e)
    console.log(finalArray)
  }

来自Arr2的所有数据都返回并与只有一半的数据Arr1合并我的数据没有返回所需的输出…我如何映射这两个数组并具有并集和交集

使用vanilla Js,您可以从两个数组中获得一个唯一的
\u id
name
数组,循环遍历该数组,并从两个数组中连接与迭代中当前
id
匹配的对象:

id
s和
name
s的重复数据消除阵列:

const ids = [...new Set([...Arr1.map(e => e._id), ...Arr2.map(e => e.name)])];
循环以连接两个数组中的元素:

const result = ids.map(e => ({
  ...Arr1.find(o => o._id === e),
  ...Arr2.find(o => o.name === e)
}))
const Arr1=[{u id:“firstSub1”,“count”:1,“price”:4},{{u id:“firstSub2”,“count”:2,“price”:7},{u id:“firstSub3”,“count”:3,“price”:1},{u id:“firstSub4”,“count”:4,“price”:1};
const Arr2=[{“name”:“firstSub1”,“date”:“05/20/1998”,“type”:“sometype1”},{“name”:“firstSub2”,“date”:“12/22/2011”,“type”:“sometype2”},{“name”:“firstSub3”,“date”:“09/23/2004”,“type”:“sometype3”},{“name”:“firstSub9”,“date”:“09/23/2004”,“type”:“sometype9”});
const-id=[…新集合([…Arr1.map(e=>e.\u-id),…Arr2.map(e=>e.name)]);
const result=ids.map(e=>({
…Arr2.find(o=>o.name==e),
…Arr1.find(o=>o.\u id==e)
}))
console.log(result)
一种解决方案是从一个
累加器开始使用,该累加器等于
Arr2
JSON.parse(JSON.stringify(Arr2))
)的副本,这样我们就不会改变原始数组
Arr2
。现在,在迭代
Arr1
时,如果找到匹配项,则将使用的相关属性添加到
累加器上的关联对象,否则将整个对象放在
累加器上:

const Arr1=[
{u id:“firstSub1”,“count”:1,“price”:4},
{u id:“firstSub2”,“count”:2,“price”:7},
{u id:“firstSub3”,“count”:3,“price”:1},
{u id:“firstSub4”,“count”:4,“price”:1}
];
常数Arr2=[
{“name”:“firstSub1”,“date”:“05/20/1998”,“type”:“sometype1”},
{“name”:“firstSub2”,“date”:“12/22/2011”,“type”:“sometype2”},
{“name”:“firstSub3”,“date”:“09/23/2004”,“type”:“sometype3”},
{“name”:“firstSub9”,“date”:“09/23/2004”,“type”:“sometype9”}
];
让res=Arr1.reduce((acc,{u id,count,price})=>
{
让fIdx=acc.findIndex(({name})=>name===\u id);
如果(fIdx>=0)
赋值(acc[fIdx],{count,price});
其他的
acc.push({u id,count,price});
返回acc;
},JSON.parse(JSON.stringify(Arr2));
控制台日志(res)
.as控制台{背景色:黑色!重要;颜色:石灰;}

.作为控制台包装{max height:100%!important;top:0;}
您可以按
名称进行排序,并比较
i+1
索引名称,以验证是否应合并
i+1
索引和
i
索引,或者对象是否没有用于合并的对

例如,按
name
排序意味着下一个对象/索引将是该对

[{name: "firstSub1", count: 1}, {name: "firstSub1", date: "dd / mm / yyyy"}] // Pair found

这是假设最多有一对

let obj={“Arr1”:[{“id”:“firstSub1”,“count”:1,“price”:4},{“id”:“firstSub2”,“count”:2,“price”:7},{“id”:“firstSub3”,“count”:3,“price”:1},{“id”:“firstSub4”,“count”:4,“price”:1}],“Arr2:[{“name”:“firstSub1”,“日期”:“1998年5月20日”,“键入”:“sometype1”},{“姓名”:“firstSub2”,“日期”:“2011年12月22日”,“键入”:“sometype2”},{“姓名”:“firstSub3”,“日期”:“2004年9月23日”,“键入”:“sometype3”},{“姓名”:“firstSub9”,“日期”:“2004年9月23日”,“键入”:“sometype9”};
//一个数组中的所有对象。
让merge=[…obj.Arr2,
//地图将按名称更改_id,这将在以后使用。
…obj.Arr1.map({u id,count,price})=>({name:{u id,count,price}))]
.sort((a,b)=>a.name.localeCompare(b.name));
//这种方法不会改变源对象。
让结果=[];
for(设i=0;i

.as console wrapper{max height:100%!important;top:0;}
您可以使用lodash的
.flow()
创建一个组合数组的函数,按
名称
\u id
对数组进行分组(在对象上找到的任何内容)。如果a组包含多个项目,则会将其合并为单个对象,并省略
\u id
属性

const{flow,partiOK:pr,concat,groupBy,map,merge,has,head,omit}=_
常数fn=流量(
concat,//合并到单个数组
pr(groupBy,o=>o.name | | o._id),//按name或_id的值分组
pr(map,o=>o.length==1?head(o):\u.omit(//如果一个组包含两个项目,则将它们合并,并删除\u id
合并({},…o),
“\u id”
)),
)
const Arr1=[{u id:“firstSub1”,“count”:1,“price”:4},{{u id:“firstSub2”,“count”:2,“price”:7},{{u id:“firstSub3”,“count”:3,“price”:1},{u id:“firstSub4”,“count”:4,“price”:1}]
const Arr2=[{“name”:“firstSub1”,“date”:“05/20/1998”,“type”:“sometype1”},{“name”:“firstSub2”,“date”:“12/22/2011”,“type”:“sometype2”},{“name”:“firstSub3”,“date”:“09/23/2004”,“type”:“sometype3”},{“name”:“firstSub9”,“date”:“09/23/2004”
[{name: "firstSub1", count: 1}, {name: "firstSub2", count: 1}] // There is no a pair