views:

57

answers:

9

ok, so I have an array like:

var myarray = {
   "field_1": "lorem ipsum",
   "field_2": 1,
   "field_2": 2,
   "field_2": 6
};

as you see there are duplicate names in the array, but with different values. If i go through it like (using jQuery):

$.each(myarray, function(key, value)
{
   console.log(key);
   console.log(myarray[key]);
   console.log(myarray[value]);
}

key - returns the correct key
myarray[key] - returns the name for that key
myarray[value] - returns the last elements', with that name, value

meaning for field_2 it will return 6, though it'll print it 3 times, as it repeats 3 times in the array.

My question is how to obtain the correct value for that duplicate named fields and not just the last one's

Thank you

A: 

The keys must be unique.

Bal
+4  A: 

That is not an array that is an object. You'd be better creating a property of the object that is an array and store the different values in there.

var myarray = {
   "field_1": "lorem ipsum",
   "field_array": []
};

myarray.field_array.push(value);

then just loop through that property of the array.

laurencek
A: 

The only way to get around it would be to either change the fields to unique identifiers, or something like:

var myarray = {
   "field_1": "lorem ipsum",
   "field_2": [
       {"value_1": 1},
       {"value_2": 2},
       {"value_3": 6}
   ]
};
Alex
A: 

You're overwriting the same value several times.

What you want is probably something like:

var myarray = {
   "field_1": "lorem ipsum",
   "field_2": [1,2,6]
};

Which could be written in a manner similar to what you currently have:

var myarray = {};

myarray.field_1 = [];
myarray.field_1.push('lorem ipsum');
myarray.field_2 = [];
myarray.field_2.push(1);
myarray.field_2.push(2);
myarray.field_2.push(6);

Note that I made field_1 an array as well, which - for consistency - I thought you might want.

David Hedlund
+1  A: 
  1. Your code has invalid syntax.
  2. There are no assocative arrays in Javascript
  3. The thing you defined is an Object
  4. If you give value to a property 3 times, sure it will contain the last value

Test

var obj = {
   "field_1": "lorem ipsum",
   "field_2": 1,
   "field_2": 2,
   "field_2": 6
};

for ( var i in obj ) {
  console.log(i + " = " + obj[i]);
}

OUTPUT

field_1 = lorem ipsum
field_2 = 6
galambalazs
A: 

You can't do this. The array key must be unique.

If you've got Firefox/Firebug installed (or similar in another browser), you can try it by entering this into the Firebug console:

var myarray = {
   "field_1": "lorem ipsum",
   "field_2": 1,
   "field_2": 2,
   "field_2": 6
};
console.dir(myarray);

Firebug will respond with:

field_1      "lorum ipsum"
field_2      6

in other words, it works, but each subsequent value specified for field_2 overwrites the previous one; you can only have one value for it at a time.

The closest you can get to what you want is to make field_2 an array in itself, something like this:

var myarray = {
   "field_1": "lorem ipsum",
   "field_2": [1,2,6]
};

If you do console.log now, you'll get this:

field_1      "lorum ipsum"
field_2
    0        1
    1        2
    2        6

Hope that helps.

Spudley
Those are not arrays, they're objects. You can call them "myarray" if you like, of course, but they're not arrays.
Pointy
@Pointy - you're right of course. I used the word 'array' because that's how it was asked in the question.
Spudley
Sorry to have worded that so rudely; I had just hurt my foot on a chair in this dark room before clicking into this question :-)
Pointy
A: 

It is not possible.

The resulting object does only contain 2 elements, the first and second field_2 elements are lost on creation.

David Mårtensson
A: 

Thanks for the replies guys. The case is that i could not change the array as it's created from the URL and in my case the duplicate names were checkboxes that are with the same name, but I figured out, a bit on an ugly one, way of breaking the URL in peaces and checking the values there and not having to work with the array.

SteeleR
There's no way you could change the name of the checkboxes? Naming a bunch of elements `field_2` isn't a great idea. Even `field_2[0]`, `field_2[1]`... is better.
clarkf
A: 

Associative arrays do not exist in Javascript - what you have created is an Object using the JSON format.

I suspect that something like this will give you more what you are seeking, though I suggest questioning exactly what it is that you are trying to achieve..

The following code will allow you to access multiple instances of duplicated 'keys', but is

var myDataset = [
   { "field_1": "lorem ipsum" },
   { "field_2": 1 },
   { "field_2": 2 },
   { "field_2": 6 }
];


$.each(myDataset, function(valuePairIndex, value)
{
    $.each(myDataset[valuePairIndex], function(key, value1)
    {
       var valuePair = myDataset[valuePairIndex];
       console.log(valuePairIndex);
       console.log(key + ' = ' + valuePair[key]);

//       console.log('key = ' + key);
//       console.log('valuePair[key] = ' + valuePair[key]);
    });
});
kwah