0

It seems impossible to me to realize this array structure

[{ "Field1" : "Foo"}, {"Field2" : "Bar" }]

with following code

    var matching = new Array();

    $('tr[type="entity"]').each(function(){

        var $select = $(this).find('select');

        matching[$select.attr('id')] = $select.val();               
    });  

This

alert(JSON.stringify(matching))

returns [ ] always. If it would be php my array would look like that

$matching = array(
    "Field1" => "Foo",
    "Field2" => "Bar"
);

I know there is no associative array in javascript. However how can I realize such an array based on my code.

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • 1
    Your php example does not match the js example on the first line. Which one are you looking for? – bfavaretto Jan 12 '13 at 20:16
  • Oh I'm a bit confused now. The structure in your first example is different than what you would get with the PHP version. What do you actually want? – Felix Kling Jan 12 '13 at 20:16

1 Answers1

4

Objects are associative arrays. Consider using var matching = new Object(); instead, and check to make sure the function is actually running (i.e. that the tr you expect actually exist).

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Or just `var matching = {};` (but of course, that's not OO if that's what you're going for). – Jordan Kasper Jan 12 '13 at 20:08
  • You know this will return `{id: value, id: value}` and not `[{id: value}, {id: value}]` as in OP's desired structured. Depending on OP's needs, this might be more suitable than his desired structured, but it is worth noting that you're returning a different structure from the intended. – Fabrício Matté Jan 12 '13 at 20:09