我有两个数组。
[
{"name":"Central", "children":
[
{"name":"Cellophane Tape","actual":"280045.5306"},
{"name":"Eraser","actual":"498573.6182"},
{"name":"File Cabinet","actual":"416966.6079"},
{"name":"File Separator","actual":"341453.1982"},
{"name":"Hard Cover File","actual":"441006.1289"},
{"name":"Highlighter","actual":"510146.4382"},
{"name":"Office Chair","actual":"473490.531"},
{"name":"Pencil","actual":"434863.2197"},
{"name":"Plastic Comb Binding","actual":"240269.3482"},
{"name":"Tape Dispenser","actual":"390286.7631"},
{"name":"White Board Markers","actual":"354810.4425"},
{"name":"Binder","actual":"265175.8668"},
{"name":"Pen","actual":"317393.0596"},
{"name":"Pen Set","actual":"408288.2822"},
{"name":"Desk","actual":"257741.0409"}
]
},
{"name":"East","children":
[
{"name":"Cellophane Tape","actual":"427724.7033"},
{"name":"Eraser","actual":"283530.3063"},
{"name":"File Cabinet","actual":"329089.8983"},
{"name":"File Separator","actual":"460416.1605"},
{"name":"Hard Cover File","actual":"425006.3076"},
{"name":"Highlighter","actual":"276329.3347"},
{"name":"Office Chair","actual":"399136.3072"},
{"name":"Pencil","actual":"424461.6597"},
{"name":"Plastic Comb Binding","actual":"258109.7709"},
{"name":"Tape Dispenser","actual":"392767.9425"},
{"name":"White Board Markers","actual":"434866.8226"},
{"name":"Binder","actual":"380534.8307"},
{"name":"Pen","actual":"513689.1382"},
{"name":"Pen Set","actual":"398575.9822"},
{"name":"Desk","actual":"265480.7438"}
]
},
{"name":"North","children":
[
{"name":"Cellophane Tape","actual":"388682.872"},
{"name":"Eraser","actual":"435463.3097"},
{"name":"File Cabinet","actual":"483667.7923"},
{"name":"File Separator","actual":"408848.6072"},
{"name":"Hard Cover File","actual":"332231.4176"},
{"name":"Highlighter","actual":"182571.9238"},
{"name":"Office Chair","actual":"479190.0605"},
{"name":"Pencil","actual":"392142.5113"},
{"name":"Plastic Comb Binding","actual":"381062.6207"},
{"name":"Tape Dispenser","actual":"324673.6576"},
{"name":"White Board Markers","actual":"265853.0888"},
{"name":"Binder","actual":"472839.831"},
{"name":"Pen","actual":"298766.3142"},
{"name":"Pen Set","actual":"442401.6033"},
{"name":"Desk","actual":"271562.3788"}
]
},
{"name":"South","children":
[
{"name":"Cellophane Tape","actual":"271934.7238"},
{"name":"Eraser","actual":"314152.7905"},
{"name":"File Cabinet","actual":"401729.4913"},
{"name":"File Separator","actual":"276306.4156"},
{"name":"Hard Cover File","actual":"514397.6782"},
{"name":"Highlighter","actual":"325500.2755"},
{"name":"Office Chair","actual":"299182.0392"},
{"name":"Pencil","actual":"434263.1176"},
{"name":"Plastic Comb Binding","actual":"374729.1889"},
{"name":"Tape Dispenser","actual":"272068.4668"},
{"name":"White Board Markers","actual":"456927.7939"},
{"name":"Binder","actual":"279658.7256"},
{"name":"Pen","actual":"291560.4142"},
{"name":"Pen Set","actual":"425946.2679"},
{"name":"Desk","actual":"275949.7598"}
]
},
{"name":"West","children":
[
{"name":"Cellophane Tape","actual":"293676.3822"},
{"name":"Eraser","actual":"300944.9422"},
{"name":"File Cabinet","actual":"402757.272"},
{"name":"File Separator","actual":"453933.4533"},
{"name":"Hard Cover File","actual":"276202.7374"},
{"name":"Highlighter","actual":"366344.7989"},
{"name":"Office Chair","actual":"249137.3409"},
{"name":"Pencil","actual":"401176.3963"},
{"name":"Plastic Comb Binding","actual":"320504.3094"},
{"name":"White Board Markers","actual":"426531.8979"},
{"name":"Binder","actual":"487622.6866"},
{"name":"Pen","actual":"425276.2638"},
{"name":"Tape Dispenser","actual":"359071.3707"},
{"name":"Pen Set","actual":"374215.8589"},
{"name":"Desk","actual":"239416.5939"}
]
}
]
第一个数组具有actual
属性,而第二个数组具有value
属性。我希望将其合并为一个。
[
{"name":"Central","children":
[
{"name":"Cellophane Tape","value":"2171.99"},
{"name":"Eraser","value":"2110.99"},
{"name":"File Cabinet","value":"2135.99"},
{"name":"File Separator","value":"2098.99"},
{"name":"Hard Cover File","value":"2021.99"},
{"name":"Highlighter","value":"2159.99"},
{"name":"Office Chair","value":"2182.99"},
{"name":"Pencil","value":"2173.99"},
{"name":"Plastic Comb Binding","value":"1476.99"},
{"name":"Tape Dispenser","value":"2062.99"},
{"name":"White Board Markers","value":"1962.99"},
{"name":"Binder","value":"2115.99"},
{"name":"Pen","value":"2073.99"},
{"name":"Pen Set","value":"2185.99"},
{"name":"Desk","value":"2096.99"}
]
},
{"name":"East","children":
[
{"name":"Cellophane Tape","value":"2039.99"},
{"name":"Eraser","value":"2063.99"},
{"name":"File Cabinet","value":"2022.99"},
{"name":"File Separator","value":"2010.99"},
{"name":"Hard Cover File","value":"2111.99"},
{"name":"Highlighter","value":"2183.99"},
{"name":"Office Chair","value":"2136.99"},
{"name":"Pencil","value":"2121.99"},
{"name":"Plastic Comb Binding","value":"2099.99"},
{"name":"Tape Dispenser","value":"2172.99"},
{"name":"White Board Markers","value":"2160.99"},
{"name":"Binder","value":"2162.99"},
{"name":"Pen","value":"2174.99"},
{"name":"Pen Set","value":"2133.99"},
{"name":"Desk","value":"2138.99"}
]
},
{"name":"North","children":
[
{"name":"Cellophane Tape","value":"2015.99"},
{"name":"Eraser","value":"2176.99"},
{"name":"File Cabinet","value":"2068.99"},
{"name":"File Separator","value":"2188.99"},
{"name":"Hard Cover File","value":"2153.99"},
{"name":"Highlighter","value":"1470.99"},
{"name":"Office Chair","value":"2092.99"},
{"name":"Pencil","value":"2126.99"},
{"name":"Plastic Comb Binding","value":"2165.99"},
{"name":"Tape Dispenser","value":"2104.99"},
{"name":"White Board Markers","value":"2141.99"},
{"name":"Binder","value":"2179.99"},
{"name":"Pen","value":"2155.99"},
{"name":"Pen Set","value":"2109.99"},
{"name":"Desk","value":"2187.99"}
]
},
{"name":"South","children":
[
{"name":"Cellophane Tape","value":"2190.99"},
{"name":"Eraser","value":"2020.99"},
{"name":"File Cabinet","value":"2178.99"},
{"name":"File Separator","value":"2142.99"},
{"name":"Hard Cover File","value":"2177.99"},
{"name":"Highlighter","value":"2093.99"},
{"name":"Office Chair","value":"2158.99"},
{"name":"Pencil","value":"2157.99"},
{"name":"Plastic Comb Binding","value":"2189.99"},
{"name":"Tape Dispenser","value":"2170.99"},
{"name":"White Board Markers","value":"2094.99"},
{"name":"Binder","value":"2168.99"},
{"name":"Pen","value":"2103.99"},
{"name":"Pen Set","value":"2181.99"},
{"name":"Desk","value":"2180.99"}
]
},
{"name":"West","children":
[
{"name":"Cellophane Tape","value":"2100.99"},
{"name":"Eraser","value":"2152.99"},
{"name":"File Cabinet","value":"2088.99"},
{"name":"File Separator","value":"2164.99"},
{"name":"Hard Cover File","value":"2064.99"},
{"name":"Highlighter","value":"2140.99"},
{"name":"Office Chair","value":"2026.99"},
{"name":"Pencil","value":"2175.99"},
{"name":"Plastic Comb Binding","value":"2014.99"},
{"name":"White Board Markers","value":"2184.99"},
{"name":"Binder","value":"2163.99"},
{"name":"Pen","value":"2151.99"},
{"name":"Tape Dispenser","value":"2040.99"},
{"name":"Pen Set","value":"2186.99"},
{"name":"Desk","value":"1928.99"}
]
}
]
我想基于每个父对象合并属性,所以我的预期输出如下所示:
[
{"name":"Central","children":
[
{"name":"Cellophane Tape","value":"2171.99","actual":"280045.5306"},
{"name":"Eraser","value":"2110.99","actual":"498573.6182"}
]
}
]
答案 0 :(得分:0)
如果您确定它们始终具有相同的结构,请尝试
var first = [
{"name":"Central", "children":
[
{"name":"Cellophane Tape","actual":"280045.5306"},
{"name":"Eraser","actual":"498573.6182"},
{"name":"File Cabinet","actual":"416966.6079"},
{"name":"File Separator","actual":"341453.1982"},
{"name":"Hard Cover File","actual":"441006.1289"},
{"name":"Highlighter","actual":"510146.4382"},
{"name":"Office Chair","actual":"473490.531"},
{"name":"Pencil","actual":"434863.2197"},
{"name":"Plastic Comb Binding","actual":"240269.3482"},
{"name":"Tape Dispenser","actual":"390286.7631"},
{"name":"White Board Markers","actual":"354810.4425"},
{"name":"Binder","actual":"265175.8668"},
{"name":"Pen","actual":"317393.0596"},
{"name":"Pen Set","actual":"408288.2822"},
{"name":"Desk","actual":"257741.0409"}
]
},
{"name":"East","children":
[
{"name":"Cellophane Tape","actual":"427724.7033"},
{"name":"Eraser","actual":"283530.3063"},
{"name":"File Cabinet","actual":"329089.8983"},
{"name":"File Separator","actual":"460416.1605"},
{"name":"Hard Cover File","actual":"425006.3076"},
{"name":"Highlighter","actual":"276329.3347"},
{"name":"Office Chair","actual":"399136.3072"},
{"name":"Pencil","actual":"424461.6597"},
{"name":"Plastic Comb Binding","actual":"258109.7709"},
{"name":"Tape Dispenser","actual":"392767.9425"},
{"name":"White Board Markers","actual":"434866.8226"},
{"name":"Binder","actual":"380534.8307"},
{"name":"Pen","actual":"513689.1382"},
{"name":"Pen Set","actual":"398575.9822"},
{"name":"Desk","actual":"265480.7438"}
]
},
{"name":"North","children":
[
{"name":"Cellophane Tape","actual":"388682.872"},
{"name":"Eraser","actual":"435463.3097"},
{"name":"File Cabinet","actual":"483667.7923"},
{"name":"File Separator","actual":"408848.6072"},
{"name":"Hard Cover File","actual":"332231.4176"},
{"name":"Highlighter","actual":"182571.9238"},
{"name":"Office Chair","actual":"479190.0605"},
{"name":"Pencil","actual":"392142.5113"},
{"name":"Plastic Comb Binding","actual":"381062.6207"},
{"name":"Tape Dispenser","actual":"324673.6576"},
{"name":"White Board Markers","actual":"265853.0888"},
{"name":"Binder","actual":"472839.831"},
{"name":"Pen","actual":"298766.3142"},
{"name":"Pen Set","actual":"442401.6033"},
{"name":"Desk","actual":"271562.3788"}
]
},
{"name":"South","children":
[
{"name":"Cellophane Tape","actual":"271934.7238"},
{"name":"Eraser","actual":"314152.7905"},
{"name":"File Cabinet","actual":"401729.4913"},
{"name":"File Separator","actual":"276306.4156"},
{"name":"Hard Cover File","actual":"514397.6782"},
{"name":"Highlighter","actual":"325500.2755"},
{"name":"Office Chair","actual":"299182.0392"},
{"name":"Pencil","actual":"434263.1176"},
{"name":"Plastic Comb Binding","actual":"374729.1889"},
{"name":"Tape Dispenser","actual":"272068.4668"},
{"name":"White Board Markers","actual":"456927.7939"},
{"name":"Binder","actual":"279658.7256"},
{"name":"Pen","actual":"291560.4142"},
{"name":"Pen Set","actual":"425946.2679"},
{"name":"Desk","actual":"275949.7598"}
]
},
{"name":"West","children":
[
{"name":"Cellophane Tape","actual":"293676.3822"},
{"name":"Eraser","actual":"300944.9422"},
{"name":"File Cabinet","actual":"402757.272"},
{"name":"File Separator","actual":"453933.4533"},
{"name":"Hard Cover File","actual":"276202.7374"},
{"name":"Highlighter","actual":"366344.7989"},
{"name":"Office Chair","actual":"249137.3409"},
{"name":"Pencil","actual":"401176.3963"},
{"name":"Plastic Comb Binding","actual":"320504.3094"},
{"name":"White Board Markers","actual":"426531.8979"},
{"name":"Binder","actual":"487622.6866"},
{"name":"Pen","actual":"425276.2638"},
{"name":"Tape Dispenser","actual":"359071.3707"},
{"name":"Pen Set","actual":"374215.8589"},
{"name":"Desk","actual":"239416.5939"}
]
}
];
var second = [
{"name":"Central","children":
[
{"name":"Cellophane Tape","value":"2171.99"},
{"name":"Eraser","value":"2110.99"},
{"name":"File Cabinet","value":"2135.99"},
{"name":"File Separator","value":"2098.99"},
{"name":"Hard Cover File","value":"2021.99"},
{"name":"Highlighter","value":"2159.99"},
{"name":"Office Chair","value":"2182.99"},
{"name":"Pencil","value":"2173.99"},
{"name":"Plastic Comb Binding","value":"1476.99"},
{"name":"Tape Dispenser","value":"2062.99"},
{"name":"White Board Markers","value":"1962.99"},
{"name":"Binder","value":"2115.99"},
{"name":"Pen","value":"2073.99"},
{"name":"Pen Set","value":"2185.99"},
{"name":"Desk","value":"2096.99"}
]
},
{"name":"East","children":
[
{"name":"Cellophane Tape","value":"2039.99"},
{"name":"Eraser","value":"2063.99"},
{"name":"File Cabinet","value":"2022.99"},
{"name":"File Separator","value":"2010.99"},
{"name":"Hard Cover File","value":"2111.99"},
{"name":"Highlighter","value":"2183.99"},
{"name":"Office Chair","value":"2136.99"},
{"name":"Pencil","value":"2121.99"},
{"name":"Plastic Comb Binding","value":"2099.99"},
{"name":"Tape Dispenser","value":"2172.99"},
{"name":"White Board Markers","value":"2160.99"},
{"name":"Binder","value":"2162.99"},
{"name":"Pen","value":"2174.99"},
{"name":"Pen Set","value":"2133.99"},
{"name":"Desk","value":"2138.99"}
]
},
{"name":"North","children":
[
{"name":"Cellophane Tape","value":"2015.99"},
{"name":"Eraser","value":"2176.99"},
{"name":"File Cabinet","value":"2068.99"},
{"name":"File Separator","value":"2188.99"},
{"name":"Hard Cover File","value":"2153.99"},
{"name":"Highlighter","value":"1470.99"},
{"name":"Office Chair","value":"2092.99"},
{"name":"Pencil","value":"2126.99"},
{"name":"Plastic Comb Binding","value":"2165.99"},
{"name":"Tape Dispenser","value":"2104.99"},
{"name":"White Board Markers","value":"2141.99"},
{"name":"Binder","value":"2179.99"},
{"name":"Pen","value":"2155.99"},
{"name":"Pen Set","value":"2109.99"},
{"name":"Desk","value":"2187.99"}
]
},
{"name":"South","children":
[
{"name":"Cellophane Tape","value":"2190.99"},
{"name":"Eraser","value":"2020.99"},
{"name":"File Cabinet","value":"2178.99"},
{"name":"File Separator","value":"2142.99"},
{"name":"Hard Cover File","value":"2177.99"},
{"name":"Highlighter","value":"2093.99"},
{"name":"Office Chair","value":"2158.99"},
{"name":"Pencil","value":"2157.99"},
{"name":"Plastic Comb Binding","value":"2189.99"},
{"name":"Tape Dispenser","value":"2170.99"},
{"name":"White Board Markers","value":"2094.99"},
{"name":"Binder","value":"2168.99"},
{"name":"Pen","value":"2103.99"},
{"name":"Pen Set","value":"2181.99"},
{"name":"Desk","value":"2180.99"}
]
},
{"name":"West","children":
[
{"name":"Cellophane Tape","value":"2100.99"},
{"name":"Eraser","value":"2152.99"},
{"name":"File Cabinet","value":"2088.99"},
{"name":"File Separator","value":"2164.99"},
{"name":"Hard Cover File","value":"2064.99"},
{"name":"Highlighter","value":"2140.99"},
{"name":"Office Chair","value":"2026.99"},
{"name":"Pencil","value":"2175.99"},
{"name":"Plastic Comb Binding","value":"2014.99"},
{"name":"White Board Markers","value":"2184.99"},
{"name":"Binder","value":"2163.99"},
{"name":"Pen","value":"2151.99"},
{"name":"Tape Dispenser","value":"2040.99"},
{"name":"Pen Set","value":"2186.99"},
{"name":"Desk","value":"1928.99"}
]
}
];
var third = first.map((item, i) => {
item.children.map((children, j) => {
children.value = second[i].children[j].value
})
return item
})
console.log(third)