Solved

Array Object pushing records

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now