我想从另一个具有相应属性标识符的对象更新对象的属性。希望我能用循环来做到这一点,但由于我的经验不足,我想不出怎么做。
我不能只说
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);
}
但electric
或gas
需要由任何标识符表示,但对象中的标识符将始终相同。
答案 0 :(得分:0)
使用以下方式排序:
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);
}
}