1
我想更新另一个对象的属性,该对象将具有相应的属性标识符。希望我能够使用循环来做到这一点,但由于缺乏经验,我无法考虑如何做到这一点。使用循环更新另一个对象的对象
我不能随便说
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
需要通过任何标识来表示,但是在对象标识符将始终是相同的。
使用'renderedCharts [electric_or_gas_or_any_variable] .real.full = ...' – 2012-03-20 18:59:00
见[此相关的问题和答案(http://stackoverflow.com/questions/9709130/使用到的javascript对象-撷取值/ 9709167#9709167) – jbabey 2012-03-20 19:00:17