简体   繁体   English

在创建子对象的同时添加键/值对

[英]Adding a key/value pair at the same time as creating a sub-object

I am new to working with objects. 我是新接触对象的人。 I want to: 我想要:

  1. Add a key 新增金钥
  2. Add a sub-object to that key 向该键添加一个子对象
  3. Push the key/value pair to this sub-object. 将键/值对推到该子对象。

For example, the data structure I am looking to achieve would be: 例如,我要实现的数据结构将是:

invalidFields = {
    "_0" : {
        "firstName" : "foo",
        "lastName" : "la",
    }
}

The addition of the most nested key/value pair is done by iterating and comparing, so in essence it needs to be added to "_0", not overwriting it. 最嵌套的键/值对的添加是通过迭代和比较完成的,因此从本质上讲,需要将其添加到“ _0”而不是覆盖它。

So far I have: 到目前为止,我有:

let invalidFields = {}; // this is initialised outside of the for loop
...
const field = "foo";
const passengerIdentifier = "_" + passengerIndex; // "_0"
const fieldKey = "firstName";
invalidFields[passengerIdentifier] = field;

This adds the key/value pair of "firstName": "foo" but when the loop continues it therefore overwrites it so the data I get is only field applied : 这将添加键/值对"firstName": "foo"但是当循环继续时,它会覆盖它,因此我得到的数据仅应用于field

invalidFields = {
    "_0" : "foo",
}

This is obviously not correct as I need the key/value pairs to add to the "_0" sub-object. 这显然是不正确的,因为我需要将键/值对添加到"_0"子对象中。 What am I doing wrong? 我究竟做错了什么?

Edit: Here is an example of what I have now, but this does not seem to work, the field part isn't added: 编辑:这是我现在拥有的示例,但这似乎不起作用,未添加字段部分:

const passengerIdentifier = '_'+passengerIndex;
if (!invalidFields[passengerIdentifier]) {
    Object.assign(invalidFields, { [passengerIdentifier] : '' });
    Object.assign(invalidFields[passengerIdentifier], { [fieldKey]: field });
}

To add key/value pair to each sub-object use the following approach: 要将键/值对添加到每个子对象,请使用以下方法:

...
var field = "foo",
    passengerIdentifier = "_" + passengerIndex, // "_0"
    fieldKey = "firstName";

// assuring sub-object with certain `passengerIdentifier`
invalidFields[passengerIdentifier] = invalidFields[passengerIdentifier] || {};
invalidFields[passengerIdentifier][fieldKey] = field;

You could use Object.assign and add the new key/value to the existing object in the loop. 您可以使用Object.assign并将新的键/值添加到循环中的现有对象。

// outside of the loop
invalidFields[passengerIdentifier] = {};

// inside of the loop
Object.assign(invalidFields[passengerIdentifier], { [fieldKey]: field });

Just for completeness, it is possible, to assign a value directly to an object 仅出于完整性考虑,可以将值直接分配给对象

// inside of the loop
invalidFields[passengerIdentifier][fieldKey] = field;
    let invalidFields = {}; // this is initialised outside of the for loop
    ...
    const field = "foo";
    const passengerIdentifier = "_" + passengerIndex; // "_0"
    const fieldKey = "firstName";

    var subObject = {
         'key1' : value1,
         'key2' : value2,
         // add all keys
     }

    invalidFields[passengerIdentifier] = subObject ;

Does this do what you're trying to achieve? 这会达到您想要达到的目的吗?

invalidFields[passengerIdentifier] = { 
  [fieldKey]: field 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM