简体   繁体   English

将密钥对添加到打字稿中的对象数组

[英]Adding keypairs to an array of objects in typescript

I have this Array of objects, and I am having problems adding more key-pairs to the objects in typescript. 我有这个对象数组,并且在向打字稿中的对象添加更多密钥对时遇到问题。

For Example I have this Array. 例如我有这个数组。

 accountsOptions:any = [
                          {'data': 
                                    {
                                      'adidas': null, 
                                      'google': null
                                    }
                          }
                        ];

And I want to add more here 我想在这里添加更多

 accountsOptions:any = [
                              {'data': 
                                        {
                                          'adidas': null, 
                                          'google': null,
                                          'nike': null,
                                          'apple': null
                                        }
                              }
                            ];

the new values are coming from another array, how can I loop(efficiently) this and dynamically add more key-pairs? 新值来自另一个数组,我该如何循环(有效)并动态添加更多密钥对? I am new to typescript. 我是打字稿新手。

You can make use of array methods and check if the object contains the property, if it does not contain add the property to the object.. 您可以使用数组方法,并检查对象是否包含该属性,如果不包含该属性,则将该属性添加到该对象。

check the following code snippet 检查以下代码片段

 "use strict"; var accountsOptions = [ { 'data': { 'adidas': null, 'google': null } } ]; function addProperty(propertyName) { accountsOptions.map(function (account) { if (!account.data.hasOwnProperty(propertyName)) { account.data[propertyName] = null; } }); } addProperty('nike'); console.log(accountsOptions) 

Hope it helps 希望能帮助到你

You get a reference to the data object: 您获得对data对象的引用:

var data:any = accountsOptions[0].data;

...and then you loop through your array, for instance with a boring old for loop (but you have lots of options there; see this question's answers to know what they are): ...然后循环遍历数组,例如,使用无聊的for循环(但那里有很多选项;请参阅此问题的答案以了解它们是什么):

for (var i:number = 0; i < yourArray.length; ++i) {
    // ...
}

...and in the loop, you use brackets notation (see this question's answers ) to add properties to the data object: ...然后在循环中,使用方括号表示法(请参阅此问题的答案 )向data对象添加属性:

data[yourArray[i]] = null;

Eg: 例如:

var data:any = accountsOptions[0].data;
for (var i:number = 0; i < yourArray.length; ++i) {
    data[yourArray[i]] = null;
}

But again, you have several options for the loop. 但同样,您有几个循环选项。 Here's another one: 这是另一个:

var data:any = accountsOptions[0].data;
yourArray.forEach((entry:string) => {
    data[entry] = null;
});

Try accountsOptions[0].data.nike = null . 尝试accountsOptions[0].data.nike = null

Obviously you can loop through accountsOptions using a for loop. 很明显,你可以通过循环accountsOptions使用for循环。

You can have more idea from these SO: Add a Key Value Pair to an array of objects in javascript? 您可以从这些SO中获得更多的想法: 向javascript中的对象数组添加键值对? and How can I add a key/value pair to a JavaScript object? 以及如何将键/值对添加到JavaScript对象?

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

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