0

I have a multi-dimensional array which I fill via $.each

$('.gBook').click(function(){
        var values = [];
        var valueToPush = { }; // or "var valueToPush = new Object();" which is the same
        var i = 0;
        $('td[data-check="true"]').each(function(){
                valueToPush["price"] = $(this).attr("data-price");
                valueToPush["id"] = $(this).attr("data-id");
                values.push(valueToPush);
                i++;
        });

        var arrayToSend = {values};

        $.post( '<?php echo PATH;?>ajax/updateRoom.php',values, function(data){
                if(data != "ERROR"){
                    $('#all-content').html(data).css("overflow-y","auto");
                }else{
                    alert("ERROR");
                }
        });
    });

But the outcome is the same for all POST vars:

Array (
  [values] => Array (
   [0] => Array ( [price] => 15 [id] => 3380 )
   [1] => Array ( [price] => 15 [id] => 3380 )
   [2] => Array ( [price] => 15 [id] => 3380 )
  ) 
) 

Where is my mistake? It seems that "push" overrides everytime?

5
  • 1
    you should initialize the var valueToPush inside the each function, you are pushing the same object "pointer" in values each time the function is executed (and updating these values) Commented Feb 24, 2015 at 15:38
  • You never used your "i" on each Commented Feb 24, 2015 at 15:38
  • 1
    @Pik_at he doesn't need i at all Commented Feb 24, 2015 at 15:46
  • That's not a multidimensional array, that's an array of objects. Commented Feb 24, 2015 at 15:47
  • i was from something else, you are right i never use it. Commented Feb 24, 2015 at 17:25

2 Answers 2

4

This line:

var valueToPush = { };

is outside your loop.

So you:

  1. create one object
  2. put a reference to it in valueToPush
  3. put values in it
  4. copy the reference to it onto the array
  5. go to 3

You only ever have one object, you just have multiple references to it in the array.

You need to create a new object each time you go around the loop.

$('td[data-check="true"]').each(function(){
    var valueToPush = {};
    valueToPush["price"] = $(this).attr("data-price");
Sign up to request clarification or add additional context in comments.

2 Comments

I understand that, but however this results in Array ( [undefined] => )
Got it, had to use $.post( '<?php echo PATH;?>ajax/updateRoom.php',[b]arrayToSend[/b], function(data){ instead of values...
0

Your each loop should simply look like this:

$('td[data-check="true"]').each(function(){
     values.push({
        price: $(this).attr("data-price"),
        id: $(this).attr("data-id")
     });
 });

You need to insert a new object in each iteration. Currently you are inserting references to a single object to into your array.

1 Comment

also "undefined" as above, starting to believe it's something else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.