当前位置:网站首页>数组对象根据id一 一对应填入(将arr1填入arr2中)
数组对象根据id一 一对应填入(将arr1填入arr2中)
2022-06-22 18:05:00 【凹凸曼与程序猿】
let arr2 = [
{
left: [
{
check: false, name: "IOU", id: 1, value: [] },
{
check: false, name: "检测框", id: 2, value: [] },
{
check: false, name: "框", id: 3, value: [] },
{
check: false, name: "属性", id: 21, value: [] },
{
check: false, name: "22x", id: 23, value: [] },
],
right: [
{
check: false, name: "aa", id: 7, value: [] },
{
check: false, name: "bb", id: 8, value: [] },
{
check: false, name: "cc", id: 9, value: [] },
],
},
];
let arr1 = [
{
left: [
{
check: true, name: "IOU", id: 1, value: ["56", "77"] },
{
check: false, name: "检测框", id: 2, value: [] },
{
check: false, name: "框", id: 3, value: [] },
],
right: [
{
check: true, name: "aa", id: 7, value: ["123555"] },
{
check: false, name: "bb", id: 8, value: [] },
{
check: false, name: "cc", id: 9, value: [] },
{
check: true, name: "cc", id: 18, value: ["132"] },
],
},
{
left: [
{
check: false, name: "IOU", id: 1, value: [] },
{
check: true, name: "检测框", id: 2, value: ["66", "7777"] },
{
check: false, name: "框", id: 3, value: [] },
],
right: [
{
check: false, name: "aa", id: 7, value: [] },
{
check: true, name: "bb", id: 8, value: [] },
{
check: false, name: "cc", id: 9, value: [] },
{
check: true, name: "cc", id: 18, value: ["132"] },
],
},
];
//方法一
const mergeArr = (param1, param2) => {
const templateArr = param1 || [];
const dataArr = param2 || [];
const filterFn = (origin = [], target = []) =>
origin.map((item = {
}) => ({
...item,
...target.filter((obj = {
}) => obj.id === item.id)[0],
}));
return dataArr.map((item = {
}) => {
const originObj = templateArr[0] || {
};
const resultObj = {
};
Object.keys(originObj).forEach((key) => {
resultObj[key] = filterFn(originObj[key], item[key]);
});
return resultObj;
});
};
let result = mergeArr(arr2, arr1);
方法二:
arr1.forEach((item1, index1) => {
item1.left.forEach((item2, index2) => {
arr2[index1].left.forEach((item) => {
if (item.id === item2.id) {
arr1[index1].left[index2] = item;
}
});
});
item1.right.forEach((item2, index2) => {
arr2[index1].right.forEach((item) => {
if (item.id === item2.id) {
arr1[index1].right[index2] = item;
}
});
});
});
console.log(123, arr1);
//方法三
const ree = arr1.map((item) => ({
left: arr2[0].left.map(
(item2) => ({
...item2,
...item.left.filter(
(obj = {
}) => obj.id === item2.id
)[0],
})
),
right: arr2[0].right.map(
(item2) => ({
...item2,
...item.right.filter(
(obj = {
}) => obj.id === item2.id
)[0],
})
),
}));
let result = ree;
边栏推荐
猜你喜欢

Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)

Shell script explanation (II) -- conditional test, if statement and case branch statement

Notes on new reports

Shell programming specification and variables

《被讨厌的勇气》读后感

C#,入门教程——关于函数参数ref的一点知识与源程序

Detailed explanation of session mechanism and related applications of session

session机制详解以及session的相关应用

How much do you know about the bloom filter and cuckoo filter in redis?

STM32 control matrix key, Hal library, cubemx configuration
随机推荐
jniLibs.srcDirs = [‘libs‘]有什么用?
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
【建议收藏】消息队列常见的使用场景
Cookie encryption 3+rpc solution
ActiveReports报表实战应用教程(十九)——多数据源绑定
3GPP 5G R17标准冻结,RedCap作为重要特性值得关注!
使能伙伴,春节重大保障“不停歇”
AUTOCAD——五种标注快捷键
RobotFramework 安装教程
Implementing Domain Driven Design - using ABP framework - solution overview
Flutter series -dart basic grammar learning
Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall
什么?HomeKit、米家、Aqara等生态也能通过智汀与天猫精灵生态联动?
200亿VS 50亿,“脱水”小红书,到底值多钱?
Problems of different renderers running on the web with flutter2.0
jniLibs. Srcdirs = ['LIBS'] what's the use?
After reading the hated courage
wpa_ CLI parameter description
集群、分布式、微服务概念和区别
输入两个字符串,输出最长相同子串