如何使用JavaScript更新或添加数组中的值?

时间:2022-04-12 07:35:14

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

我在JavaScript中使用变量中的数组数组。我想用子数组更新一个值,而不必临时复制数组。

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.

如果存在,我希望能够更改123键内部的值。否则,我想创建该密钥。此外,如果子数组中不存在'a',我想添加它而不是抛出错误。

I am trying to update

我正在尝试更新

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

but since assigned_at does not exists I am getting this error

但由于assigned_at不存在,我收到此错误

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

无法设置未定义或空引用的属性“assigned_at”。

How can I correctly update the array correctly.

如何正确更新阵列?

3 个解决方案

#1


6  

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

你的问题不在于assigned_at不存在,而是数据['123']不存在,因此未定义。

Try data[0]['123'].

尝试数据[0] ['123']。

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

BTW:你似乎不理解数组和/或对象,因为你在每个数组中放置了一个对象。

#2


5  

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

您可能希望在这种情况下最简单的数据形式是JSON,因此括号是不必要的:

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:

你所拥有的是一个包含一个JSON元素的数组,所以你可以使用以下代码获得它:

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

#3


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:

所以你的问题是你认为你正在使用一个hashmap,而实际上你正在使用一个数组。所以当你写下这样的东西时:

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}].

这将打印[{'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

如果你想将一个新密钥分配给data [0] [“123”],既然你将值包装在一个数组中,你还需要编写

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.

哪个会奏效。我认为你只是通过将[]添加到你的价值观中而让自己有点混乱。

#1


6  

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

你的问题不在于assigned_at不存在,而是数据['123']不存在,因此未定义。

Try data[0]['123'].

尝试数据[0] ['123']。

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

BTW:你似乎不理解数组和/或对象,因为你在每个数组中放置了一个对象。

#2


5  

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

您可能希望在这种情况下最简单的数据形式是JSON,因此括号是不必要的:

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:

你所拥有的是一个包含一个JSON元素的数组,所以你可以使用以下代码获得它:

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

#3


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:

所以你的问题是你认为你正在使用一个hashmap,而实际上你正在使用一个数组。所以当你写下这样的东西时:

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}].

这将打印[{'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

如果你想将一个新密钥分配给data [0] [“123”],既然你将值包装在一个数组中,你还需要编写

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.

哪个会奏效。我认为你只是通过将[]添加到你的价值观中而让自己有点混乱。