Javascript 使用循环从另一个对象更新对象
我想从另一个具有相应属性标识符的对象更新对象的属性。希望我能用一个循环来完成,但由于我缺乏经验,我无法思考如何做到这一点 我不能这么说Javascript 使用循环从另一个对象更新对象,javascript,loops,object,properties,identifier,Javascript,Loops,Object,Properties,Identifier,我想从另一个具有相应属性标识符的对象更新对象的属性。希望我能用一个循环来完成,但由于我缺乏经验,我无法思考如何做到这一点 我不能这么说 object1 = object2; 因为对象中的数组正在连接 我目前拥有的是这样的: if (direction === 'forw') { renderedCharts.electric.real.full = renderedCharts.electric.real.full.concat(newChartData.electric.real);
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);
}
但是电气或气体需要用任何标识符表示,但对象中的标识符始终相同。使用以下方法排序:
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);
}
}
使用
渲染图[electric\u或\u gas\u或任何变量].real.full=…
请参阅