Solved

Array Object pushing records

Posted on 2014-12-07
1
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 82
SharePoint and CAML query help 4 26
JS to redirect to prev page 8 47
Drag & Drop Error 5 32
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…

751 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