Solved

Array Object pushing records

Posted on 2014-12-07
1
69 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now