使用循环从另一个对象更新对象

Update an object from another object using a loop

本文关键字:一个对象 更新 对象 循环      更新时间:2023-09-26

我想更新另一个具有相应属性标识符的对象的属性。希望我能用一个循环来做到这一点,但由于我缺乏经验,我想不出该怎么做。

我不能只说

object1 = object2;

因为对象中的数组正在连接。

我目前拥有的是这样的:

if (direction === 'forw')
{
    renderedCharts.electric.real.full = renderedCharts.electric.real.full.concat(newChartData.electric.real);
    renderedCharts.electric.budget.full = renderedCharts.electric.budget.full.concat(newChartData.electric.budget);
    renderedCharts.gas.real.full = renderedCharts.gas.real.full.concat(newChartData.gas.real);
    renderedCharts.gas.budget.full = renderedCharts.gas.budget.full.concat(newChartData.gas.budget);
}
else if (direction === 'back')
{
    for (var index in newChartData) // get the length of the arrays returned (all arrays will have the same number of elements
    {
        dataOffset += newChartData[index].real.length;
        break;
    }
    renderedCharts.electric.real.full = newChartData.electric.real.concat(renderedCharts.electric.real.full);
    renderedCharts.electric.budget.full = newChartData.electric.budget.concat(renderedCharts.electric.budget.full);
    renderedCharts.gas.real.full = newChartData.gas.real.concat(renderedCharts.gas.real.full);
    renderedCharts.gas.budget.full = newChartData.gas.budget.concat(renderedCharts.gas.budget.full);
}

但是CCD_ 1或CCD_。

使用排序

if (direction === 'forw')
{
    for (var property in renderedCharts)
    {
        renderedCharts[property].real.full = renderedCharts[property].real.full.concat(newChartData[property].real);
        renderedCharts[property].budget.full = renderedCharts[property].budget.full.concat(newChartData[property].budget);
    }
}
else if (direction === 'back')
{
    for (var property in newChartData)
    {
        dataOffset += newChartData[property].real.length;
        break;
    }
    for (var property in renderedCharts)
    {
        renderedCharts[property].real.full = newChartData[property].real.concat(renderedCharts[property].real.full);
        renderedCharts[property].budget.full = newChartData[property].budget.concat(renderedCharts[property].budget.full);
    }
}