Solved

Array Object pushing records

Posted on 2014-12-07
1
72 Views
Last Modified: 2014-12-08
If you look at the commented variable var dataSource; I am trying to reproduce this but the code isn't working when logging it doesn't display the second record with the proper values;

//var dataSource = [{ time: 1730, pwr: 546},{ time: 1731, pwr: 705}];

var chartData=[];
var chartRecord={}

chartRecord["time"] = 1730;
chartRecord["pwr"] = 546;

chartData.push(chartRecord);

chartRecord["time"] = 1731;
chartRecord["pwr"] = 745;

chartData.push(chartRecord);

console.log(chartData);

Open in new window

0
Comment
Question by:stargateatlantis
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40485787
A reference to chartRecord is added to the array so you need to re-initialize it to an empty object after adding it to the array.
var chartData=[];
var chartRecord={};

chartRecord["time"] = 1730;
chartRecord["pwr"] = 546;

chartData.push(chartRecord);

// the first object is in the array now start with a new object

chartRecord={};
chartRecord["time"] = 1731;
chartRecord["pwr"] = 745;

chartData.push(chartRecord);

console.log(chartData);

Open in new window

EDIT: to be more exact, a copy of the object reference in the variable chartRecord is added to the array. This is why making chartRecord reference a new object works, while in the original situation, everything points to the same object and changing the properties does not have the intended effect.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question