当前位置:网站首页>The array objects are filled in one by one according to the ID (fill Arr1 into arr2)
The array objects are filled in one by one according to the ID (fill Arr1 into arr2)
2022-06-22 19:43:00 【Attapulgite and procedural ape】
let arr2 = [
{
left: [
{
check: false, name: "IOU", id: 1, value: [] },
{
check: false, name: " Check box ", id: 2, value: [] },
{
check: false, name: " box ", id: 3, value: [] },
{
check: false, name: " attribute ", 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: " Check box ", id: 2, value: [] },
{
check: false, name: " box ", 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: " Check box ", id: 2, value: ["66", "7777"] },
{
check: false, name: " box ", 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"] },
],
},
];
// Method 1
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);
Method 2 :
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);
// Method 3
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;
边栏推荐
猜你喜欢

Activereports report practical application tutorial (19) -- multi data source binding

故障分析 | 从 data_free 异常说起

Flutter series -flutter route management

Iplook and SBC establish long-term cooperation

Detailed explanation of shell script (x) -- how to use sed editor

Adapter mode of structural mode

《被讨厌的勇气》读后感

About Random Forest

Problems of different renderers running on the web with flutter2.0

Openpnp使用过程的一些问题记录
随机推荐
Altium Designer中off grid pin解决方法
Shell programming specification and variables
Do you use thread or service?
shell脚本详解(二)——条件测试、if语句和case分支语句
1.2-----机械设计工具(CAD软件)和硬件设计工具(EDA软件)及对比
NRF51822外设学习
远程访问及控制——SSH远程管理及TCP Wrappers 访问控制
Interface development component devaxpress asp Net core v21.2 - UI component enhancements
About Random Forest
结构型模式之代理模式
将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT
数字赋能机械制造业,供应链协同管理系统解决方案助力企业供应链再升级
插槽里如何判断text为数组
Paopao Mart: empty souls need stories
vim中快速缩进用法
Quick indent usage in VIM
Typescript (7) generic
Active directory user logon Report
Common technical notes
3D打印机耗材受潮