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

하지만 electric 또는 gas 어떤 식별자로 표현 될 수 있지만, 객체의 식별자는 항상 동일합니다 필요합니다.

+1

사용은'renderedCharts는 [electric_or_gas_or_any_variable] .real.full = ...' –

+0

[이 관련 질문과 답변 (http://stackoverflow.com/questions/9709130/ 참조 자바 스크립트 개체 - 값을 사용하여 검색/9709167 # 9709167) – jbabey

답변

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); 
    } 
} 
관련 문제