3

I have an array of arrays. I want to be able to loop over each array and for every array I want to add new keys or update existing values.

Here is what I have

    var values = [];

    values['123'] = [];
    values['456'] = [];
    values['123']['x1'] = 'value 1';
    values['123']['x2'] = 'value 2';

I want to loop over the values array, and add new keys to the array for each array. (I.e values['123'] and values['456'])

Here is what I tried

$.each(values, function(index, value){

  value['x1'] = 'new value 1';
  value['x10'] = 'new value 10';
  value['x20'] = 'new value 20';
  console.log(value);

});

The console shows this error

TypeError: value is undefined

Here is a fiddle

How can I correcly loop over each array item and update the original array?

2 Answers 2

1

Actually for your case you need to use Object, not Array

For constructing non-numeric indexes you should use {}

{} - for constructing Object, [] - for constructing Array

jQuery.each() can be used to iterate over both objects and arrays.

Try this code

$(function() {
    $('#x').click(function(e) {
        var values = {}; // here

        values['123'] = {}; // here
        values['456'] = {}; // here
        values['123']['x1'] = 'value1';
        values['123']['x2'] = 'value2';


        $.each(values, function(index, value) {

            value['x1'] = 'new value 1';
            value['x10'] = 'new value 10';
            value['x20'] = 'new value 20';
            console.log(value);

        });
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

That worked! Can you please tell me what is the difference between {} and [] ?
@MikeA Here, values is being initialised as an object (see the {}) rather than an array. $.each() function treats 123 & 456 as keys in case of object, however, in case of array it treats them as indexes. Thats why I added the values[index]!== undefined condition in my code below.
Thank you very much for your help
@MikeA I've added some links and info
0

This is happening because your values array is getting initialized with indexes 123 and 456. So, the $.each() function assumes the array length to be 457 and thus starts from element index 0, though there is no value at that index.

In order to overcome this, you can simply make the following change and it will work-

$.each(values, function(index, value){
    if(values[index] !== undefined) { 
        value['x1'] = 'new value 1';
        value['x10'] = 'new value 10';
        value['x20'] = 'new value 20';

        //Update the new values in the values object.
        values[index] = value;
        console.log(value);
    }
});

3 Comments

That removed the error but values is now empty and it should not.
Check my updated answer. The values object was not getting updated with the latest values earlier. It should work as required now.
As per your question, since you asked how to loop an array of arrays, this should be the answer. Since here, I didn't make any changes in your array structure. The current accepted answer makes a change in the underlying structure by simply changing the main array into object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.