user3601092 user3601092 - 1 year ago 72
HTML Question

Pushing objects in an array only returns last object pushed

I am working on YUI DataTables, to populate the data table i want to send the columns and their values dynamically from outside, for that i have written the following code:

<script src=""></script>
<div class="example yui3-skin-sam" id="simple"> <!-- You need this skin class -->

var cols = ["id", "name", "price"];
var obj = {
id: "",
name: "",
price: ""
var data2 = new Array(); = "ga"; = "gadget";
obj.price = "$6.99";
data2.push(obj); = "ga2"; = "gadget2";
obj.price = "$7.99";
data2.push(obj); = "ga3"; = "gadget3";
obj.price = "$8.99";
YUI().use("datatable", function (Y) {
// A table from data with keys that work fine as column names
var simple = new Y.DataTable({
columns: cols,
data : data2,
summary: "Price sheet for inventory parts",
caption: "Example table with simple columns"



now the problem is that, it shows only the last obj pushed in data2.. kindly tell me why its not displaying all three objs. the resulting table from this code is

id name price

ga3 gadget3 $8.99

Answer Source

it will give all array values

function obj(id, name, price) { = id; = name;
     this.price = price;

var array = new Array();

var point = new obj("ga", "gadget", "$6.99");


var point = new obj("ga2", "gadget2", "$7.9");


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download