2

我有一个这样的数组->

var jsonResponse = [
     {   
        "name": "abc",
        "value": [
            { "label" : "Daily", "value":"Daily"}         
        ]
    },
    {   
        "name": "ccc",
        "value": [
            { "label" : "Daily", "value":"Daily"}         
        ]
    }
]

我想将其转换为 ->

{
    "abc" : {   
        "name": "abc",
        "value": [
            { "label" : "Daily", "value":"Daily"}         
        ]
    },
    "ccc": {   
        "name": "ccc",
        "value": [
            { "label" : "Daily", "value":"Daily"}         
        ]
    }
]

可能我不想要foreach。我们可以使用 Object.assign(arrayDetails, ...jsonResponse); 但是如何做对象索引呢?

4

1 回答 1

1
let indexedResult = {};
jsonResponse.map(obj => indexedResult[obj.name] = obj)

console.log(JSON.stringify(indexedResult));
于 2017-12-09T06:41:31.297 回答