将具有一些新属性的对象数组添加到另一个具有相同名称的对象数组
Adding an Array of Objects with some new properties to another Array of Objects which has same name
我的 redux 存储中有一个名为“值”的对象数组,这是我通过在每次进程启动和停止时向其添加启动 time/end 时间来新创建的。看起来像这样
const value = [
{startTime:1635926165, name:"dailyScripts", endTime:1635926189},
{startTime:1635926125, name:"datawarehouse", endTime:1635926125},
{startTime:1234567890, name:"dummp", endTime:1234567890}
]
我在名为列表的“本地状态”中有另一个对象数组,其中包含所有数据。这里我想给每个和上面同名的obj加上开始时间和结束时间
const list = [
{pid: 248, name: 'dailyScripts', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 259, name: 'datawarehouse', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 0, name: 'dump', pm2_env: {…}, pm_id: 2, monit: {…}}
]
所以我的最终输出应该是这样的 -
const list = [
{pid: 248, name: 'dailyScripts',...otherObjs, startTime:1635926165, endTime:1635926189}
{pid: 259, name: 'datawarehouse',...otherObjs, startTime:1635926125, endTime:1635926125}
{pid: 0, name: 'dump',...otherObjs, startTime:1635926165, endTime:1635926189}
]
我该如何使用 React 来实现它?我想改变列表状态或将值 arr 与列表 arr 合并。
试试这个:
const value = [
{ startTime: 1635926165, name: "dailyScripts", endTime: 1635926189 },
{ startTime: 1635926125, name: "datawarehouse", endTime: 1635926125 },
{ startTime: 1234567890, name: "dummp", endTime: 1234567890 }
];
const list = [
{ pid: 248, name: 'dailyScripts', pm2_env: {}, pm_id: 2, monit: {} },
{ pid: 259, name: 'datawarehouse', pm2_env: {}, pm_id: 2, monit: {} },
{ pid: 0, name: 'dump', pm2_env: {}, pm_id: 2, monit: {} }
];
let output = [];
list.forEach(e => {
let valueTime = value.find(ee => ee.name === e.name);
if (valueTime) {
output.push({...e, startTime : valueTime.startTime, endTime : valueTime.endTime });
} else {
output.push({...e});
}
});
console.log(output);
我的 redux 存储中有一个名为“值”的对象数组,这是我通过在每次进程启动和停止时向其添加启动 time/end 时间来新创建的。看起来像这样
const value = [
{startTime:1635926165, name:"dailyScripts", endTime:1635926189},
{startTime:1635926125, name:"datawarehouse", endTime:1635926125},
{startTime:1234567890, name:"dummp", endTime:1234567890}
]
我在名为列表的“本地状态”中有另一个对象数组,其中包含所有数据。这里我想给每个和上面同名的obj加上开始时间和结束时间
const list = [
{pid: 248, name: 'dailyScripts', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 259, name: 'datawarehouse', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 0, name: 'dump', pm2_env: {…}, pm_id: 2, monit: {…}}
]
所以我的最终输出应该是这样的 -
const list = [
{pid: 248, name: 'dailyScripts',...otherObjs, startTime:1635926165, endTime:1635926189}
{pid: 259, name: 'datawarehouse',...otherObjs, startTime:1635926125, endTime:1635926125}
{pid: 0, name: 'dump',...otherObjs, startTime:1635926165, endTime:1635926189}
]
我该如何使用 React 来实现它?我想改变列表状态或将值 arr 与列表 arr 合并。
试试这个:
const value = [
{ startTime: 1635926165, name: "dailyScripts", endTime: 1635926189 },
{ startTime: 1635926125, name: "datawarehouse", endTime: 1635926125 },
{ startTime: 1234567890, name: "dummp", endTime: 1234567890 }
];
const list = [
{ pid: 248, name: 'dailyScripts', pm2_env: {}, pm_id: 2, monit: {} },
{ pid: 259, name: 'datawarehouse', pm2_env: {}, pm_id: 2, monit: {} },
{ pid: 0, name: 'dump', pm2_env: {}, pm_id: 2, monit: {} }
];
let output = [];
list.forEach(e => {
let valueTime = value.find(ee => ee.name === e.name);
if (valueTime) {
output.push({...e, startTime : valueTime.startTime, endTime : valueTime.endTime });
} else {
output.push({...e});
}
});
console.log(output);