1

I have an array of arrays in a variable using in JavaScript. I would like to update the value with the sub-array without having to copy the array temporary.

Here is an example.

I have this array

var data= [{
             '123':  [{'a': 10, 'b': 20, 'c': 30, 'd': 40}],
             '456':  [{'a': 1, 'b': 2, 'c': 3, 'd': 4}]
          }];

I want to be able to change the value of a inside of the 123 key if a existing. Otherwise, I want to create that key. Also, if 'a' does not exists in the sub-array, I want to add it instead of throwing an error.

I am trying to update

data['123']['assigned_at'] = 'value';

but since assigned_at does not exist I am getting this error

Unable to set property 'assigned_at' of undefined or null reference.

How can I correctly update the array correctly?

var data = [{
  '123': [{
    'a': 10,
    'b': 20,
    'c': 30,
    'd': 40
  }],
  '456': [{
    'a': 1,
    'b': 2,
    'c': 3,
    'd': 4
  }]
}];

data['123']['assigned_at'] = 'value';

1
  • It appears data is an array containing a single object which in return holds the keys 123, 456. Is that a typo? Oh and 123 and 456 are again arrays with only a single object - so the full path would be: data[0][123][0]["a"] Commented Mar 7, 2016 at 21:45

3 Answers 3

6

Your problem is not that assigned_at does not exist, it's that data['123'] does not exist, hence undefined.

Try data[0]['123'].

BTW: you seem to not understand arrays and/or objects since you put a single object in each of your arrays.

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

2 Comments

Sorry, I think my example just throw everything off. Here is how I define my array var data = []; Then I add a key and array to it data['123']['assigned_at'] = 'value'. I hope this better explain what I am trying to do.PS: data[0]['123'] is still now working.
@MikeA how about you ask about your problem instead of telling that you have something which you really don't? And while you're at it, create a MCVE
5

You probably want the simpliest form of the data which would be a JSON in this case, so the brackets are unnecessary:

var data= {
             '123': {'a': 10, 'b': 20, 'c': 30, 'd': 40},
             '456': {'a': 1, 'b': 2, 'c': 3, 'd': 4}
          };

Now you can get the items like this:

data['123'];
// and the nested objects:
data['123']['a'];

What you had was an Array with one JSON element in it, so you would get it with the following code:

data[0]['123']
// and for the nested objects:
data[0]['123'][0]['a']

Comments

2

So you're problem is that you think you are using a hashmap when in reality you are using an array. So when you write something like:

var data= [{
         '123':  [{'a': 10, 'b': 20, 'c': 30, 'd': 40}],
         '456':  [{'a': 1, 'b': 2, 'c': 3, 'd': 4}]
      }];

console.log(data["123"])

You'll get an error because you are trying to index an array with a string. As you can see by looking at your data object, it's an array wrapping a hashmap (which is honestly unnecessary). If you really were to use this object correctly you'd need to write

var data= [{
         '123':  [{'a': 10, 'b': 20, 'c': 30, 'd': 40}],
         '456':  [{'a': 1, 'b': 2, 'c': 3, 'd': 4}]
      }];

console.log(data[0]["123"])

which will print [{'a': 10, 'b': 20, 'c': 30, 'd': 40}].

If you wanted to assign a new key into data[0]["123"], since you wrap the value in an array as well, you'd need to write

var data= [{
         '123':  [{'a': 10, 'b': 20, 'c': 30, 'd': 40}],
         '456':  [{'a': 1, 'b': 2, 'c': 3, 'd': 4}]
      }];

data[0]["123"][0]["assigned_at"] = 52 //or w/e you want here

I'd recommend dropping the [] wrapping around your values and just writing

var data= {
         '123':  {'a': 10, 'b': 20, 'c': 30, 'd': 40},
         '456':  {'a': 1, 'b': 2, 'c': 3, 'd': 4}
      };

data["123"]["assigned_at"] = 52 //or w/e

Which will work. I think you've just caused yourself a little confusion by adding the [] to your values is all.

Comments

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.