Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push values to array in jquery

I have List of items data and empty array quotations :

   var data = {};
   var quotations = [];

I want to fill quotations with data values ,Every time i add new data it added successfully but all data values get last value .

for example :

 $("#addquotation").click(function () {
        debugger;
        var itemname = $("#itemname").val();
        var cost =parseFloat( $("#cost").val());
        var notes = $("#notes").val();
        var date = $("#date").val();


        data.Item = itemname;
        data.Cost = cost;
        data.Notes = notes;
        data.Date = date;

        quotations.push(data);
)};

for first time i add

"test,45,testnotes,2016-02-03" Second time i 've added "test2,45.2,testnotes2,2016-02-05"

when i debug i get data as :

obj(0): "test2,45.2,testnotes2,2016-02-05" obj(1):"test2,45.2,testnotes2,2016-02-05"

it seems it append last version to all data

Please Advice . Thanks

like image 910
prime Avatar asked Mar 20 '16 20:03

prime


People also ask

How do you push variables into an array?

The arr. push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.

What is push in jQuery?

The array push() function is a built-in function in jQuery. The array push() function pushes one or more elements to the end of an array and returns the new length of an array, with the help of the array push() function the adding elements to an array becomes very easy.

How do you push an element to an array at first position?

The unshift() method adds new elements to the beginning of an array.

How do you push key value?

Use map() to Push Key-Value Pair Into an Array in JavaScript The ECMAScript6 (ES6) introduces the arrow functions that let us write the function more concisely. We can only use this function if the function has only one statement.


Video Answer


3 Answers

You need to declare data inside the click handler, if it's declared as a global variable you are basically always modifying and adding the same data object to the array:

var quotations = [];

$("#addquotation").click(function () {
        debugger;
        var data = {};
        var itemname = $("#itemname").val();
        var cost =parseFloat( $("#cost").val());
        var notes = $("#notes").val();
        var date = $("#date").val();


        data.Item = itemname;
        data.Cost = cost;
        data.Notes = notes;
        data.Date = date;

        quotations.push(data);
)};
like image 54
HaukurHaf Avatar answered Oct 14 '22 15:10

HaukurHaf


You are pushing the same object reference each time since you declared data outside of the click handler.

Change from :

var data={};
$("#addquotation").click(function () {

To

$("#addquotation").click(function () {
     var data={};// declare local variable
like image 26
charlietfl Avatar answered Oct 14 '22 15:10

charlietfl


The problem is that data is a global variable and you add a reference to data to quotations.

When the first value is pushed to quotations, data and quotations[0] refer to the same object. Here is an example of what is happening:

var a = {num: 1};
var b = a;
b.num = 2;
console.log(a.num); // prints 2

The same thing happens when an object is pushed to an array. quotations does not contain a copy of data, it contains a reference to data so that modifying data also modifies quotations. To fix this, each element of quotations must refer to a different data object. This can be accomplished by defining data inside of the function instead of outside.

Replace

var data = {};
$("#addquotation").click(function() {
    // populate data, push to quotations
});

with

$("#addquotation").click(function() {
    var data = {};
    // populate data, push to quotations
});
like image 34
afuous Avatar answered Oct 14 '22 14:10

afuous