在Javascript中添加数组键值

时间:2021-04-16 20:19:01

I have a network array like the following way

我有一个像以下方式的网络数组

"network_contents": [

      {
         "facebook":"contents to all pages",

      },
      {
         "twitter":"twiter contents",

      },
      {
         "linkedin":"linked in contents",

      }
]

I would like to add some keys to that array bases on its content. If it is facebook the key should be facebook, if it is twitter key should be twitter. But not sure how to do it.

我想在其内容的基础上添加一些关键字。如果是facebook,那么键应该是facebook,如果是twitter键应该是twitter。但不知道该怎么做。

My requirement is to access network array contents, but it may or may not content these facebook, twitter, linked in values. I need to access its values. When i assign a key value will be easy to fetch its contents. So i tried this way to loop through the array

我的要求是访问网络数组内容,但它可能会或可能不会将这些facebook,twitter与值相关联。我需要访问它的值。当我分配一个键值时,将很容易获取其内容。所以我尝试这种方式循环数组

message.network_contents.forEach( function (nwContent) {
                        if(nwContent.twitter) {
                            console.log('nw content', nwContent.twitter);
                        }

                    })

can i create an array in this foreach loop like the following way.

我可以在这个foreach循环中创建一个数组,如下所示。

{
    "data": [
        {
            "facebook": {
                "facebook": "facebook content"
            },
            "twitter": {
                "twitter": "twitter content"
            }
        }
    ]
}

Your help is much appreciated thanks

非常感谢您的帮助

2 个解决方案

#1


1  

Implementation of what I said in the comment:

实施我在评论中所说的内容:

var oldsies = stuff.network_contents;
var newsies = stuff.network_contents = {};
oldsies.forEach(function(network) {
  var name = Object.keys(network)[0];
  newsies[name] = network;
});

#2


0  

You gave an example of a JS object and not a dictionary and therefore cant add key-values. You need something like this:

您给出了JS对象的示例而不是字典,因此无法添加键值。你需要这样的东西:

    var network_contents = [];
    network_contents["facebook"] = {config1: {name:"config1", value:"value1"}};
    network_contents["twitter"] = {config2: {name:"config2", value:"value2"}};

example: network_contents["facebook"].config1.value; // will return "value1"

例如:network_contents [“facebook”]。config1.value; //将返回“value1”

You can covert your object to a dictionary easily.

您可以轻松地将对象转换为字典。

#1


1  

Implementation of what I said in the comment:

实施我在评论中所说的内容:

var oldsies = stuff.network_contents;
var newsies = stuff.network_contents = {};
oldsies.forEach(function(network) {
  var name = Object.keys(network)[0];
  newsies[name] = network;
});

#2


0  

You gave an example of a JS object and not a dictionary and therefore cant add key-values. You need something like this:

您给出了JS对象的示例而不是字典,因此无法添加键值。你需要这样的东西:

    var network_contents = [];
    network_contents["facebook"] = {config1: {name:"config1", value:"value1"}};
    network_contents["twitter"] = {config2: {name:"config2", value:"value2"}};

example: network_contents["facebook"].config1.value; // will return "value1"

例如:network_contents [“facebook”]。config1.value; //将返回“value1”

You can covert your object to a dictionary easily.

您可以轻松地将对象转换为字典。