2

When I am trying to push object on the array its duplicate all the object to the last pushed object.

 var seriesArr = [];
 var seriesDemo = {};

 var seriesFinal = finalArr[0]

 for (var o in finalArr[0]) {
    seriesDemo.valueField = o;
    seriesArr.push(seriesDemo);
 }

OUTPUT:

[{"valueField":"amount[3]"},{"valueField":"amount[3]"},{"valueField":"amount[3]"},{"valueField":"amount[3]"}]

It should be like: [{"valueField":"amount[0]"},{"valueField":"amount[1]"},{"valueField":"amount[2]"},{"valueField":"amount[3]"}]
2
  • What is value of finalArr ? Commented Jun 8, 2016 at 6:40
  • forget about seriesDemo object and do like seriesArr.push({valueField:o}); Commented Jun 8, 2016 at 7:11

1 Answer 1

4

Maybe you are looking for this?

for (var o in finalArr[0]) {
   var seriesDemo = {}; // (Re-)Initialize here
   seriesDemo.valueField = o;
   seriesArr.push(seriesDemo);
}

The problem is that you are updating the global seriesDemo hash everytime and it is pushed into seriesArr by reference. So all entries in seriesArr are holding reference to the last entry.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.