1

I want to update properties of objects from another object which will have corresponding property identifiers. Hopefully I will be able to use a loop to do so but due to my inexperience I cannot think how to do this.

I cannot just say

object1 = object2;

because the arrays in the object are being concatenated.

What I currently have looks like this:

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);
}

but electric or gas need to be represented by any identifier but the identifiers in the objects will always be the same.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35
  • 1
    Use `renderedCharts[electric_or_gas_or_any_variable].real.full = ...` – Rob W Mar 20 '12 at 18:59
  • see [this related question and answer](http://stackoverflow.com/questions/9709130/javascript-object-using-to-retrieve-values/9709167#9709167) – jbabey Mar 20 '12 at 19:00

1 Answers1

0

Sorted using:

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);
    }
}
Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35