如何将两个对象合并为一个并更新合并对象中的记录总数

问题描述 投票:0回答:2

我需要合并两个对象。我尝试在

concat
数组上使用
items
方法,但由于附加属性(例如
count: 5, total_records : "3", offset: 500, has_more: false 
存在于我的物体中。

我也想在我的合并对象中保留这些属性。我特别想通过更新合并对象的

items
count
值来跟踪合并的
total_records
数组中的总对象/记录计数。

对于下面的示例代码,如果第一个对象的

items
数组包含 3 个项目,而第二个对象的
items
数组仅包含 2 个项目,那么除了串联的
items
数组之外,最终合并的对象还应显示 .. .

count: 5,
total_records: "5",
offset: 500,
has_more: false,

请建议如何实施。以下是我的示例数据。

const arr1 = {
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
  ],
  count: 3,
  total_records: "3",
  offset: 500,
  has_more: false,
};
const arr2 = {
  items: [
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 2,
  total_records: "2",
  offset: 500,
  has_more: false,
};

预期的结果是......

{
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 5,
  total_records: "5",
  offset: 500,
  has_more: false,
}
javascript object data-structures merge concatenation
2个回答
2
投票

为了快速集成

const arr1 = {
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
  ],
  count: 3,
  total_records: "3",
  offset: 500,
  has_more: false,
};
const arr2 = {
  items: [
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 2,
  total_records: "2",
  offset: 500,
  has_more: false,
};

console.log({
    items: [...arr1.items, ... arr2.items],
    count: arr1.count + arr2.count,
    total_records: (arr1.count + arr2.count).toString(),
    offset: arr1.offset,
    has_more: arr1.has_more || arr2.has_more
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

并且具有不可否认的事实来源

const arr1 = {
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
  ],
  count: 3,
  total_records: "3",
  offset: 500,
  has_more: false,
};
const arr2 = {
  items: [
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 2,
  total_records: "2",
  offset: 500,
  has_more: false,
};

const newArray = [...arr1.items, ... arr2.items]
console.log({
    items: newArray,
    count: newArray.length,
    total_records: newArray.length.toString(),
    offset: arr1.offset,
    has_more: arr1.has_more || arr2.has_more
});
.as-console-wrapper { min-height: 100%!important; top: 0; }


-1
投票

你可以这样做:

var jsonArray = {items: []};
jsonArray.items = jsonArray1.items.concat(jsonArray2.items);
console.log(jsonArray);
© www.soinside.com 2019 - 2024. All rights reserved.