2012-03-20 98 views
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); 
} 

electricgas需要通过任何标识来表示,但是在对象标识符将始终是相同的。

+1

使用'renderedCharts [electric_or_gas_or_any_variable] .real.full = ...' – 2012-03-20 18:59:00

+0

见[此相关的问题和答案(http://stackoverflow.com/questions/9709130/使用到的javascript对象-撷取值/ 9709167#9709167) – jbabey 2012-03-20 19:00:17

回答

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); 
    } 
} 
相关问题