How to create dictionary and add key–value pairs dynamically?

后端 未结 15 738
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 00:37

From post:

Sending a JSON array to be received as a Dictionary

I’m trying to do this same thing as that post. The only issue is that I d

15条回答
  •  眼角桃花
    2020-11-28 01:19

    Since you've stated that you want a dictionary object (and not an array like I assume some understood) I think this is what you are after:

    var input = [{key:"key1", value:"value1"},{key:"key2", value:"value2"}];
    
    var result = {};
    
    for(var i = 0; i < input.length; i++)
    {
        result[input[i].key] = input[i].value;
    }
    
    console.log(result); // Just for testing
    

提交回复
热议问题