converting array to json object

后端 未结 2 936
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 11:09

I have an array in java script .. something like: var myarray = [\'a\',\'b\',c\']; and var item = \'Name\'; and I want to convert that to something lik

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 11:43

    var result = myarray.reverse().reduce(function (value, key) {
        var result = {};
        result[key] = value;
        return result;
    }, { item : item });
    

    In other words, you're packing the result layer by layer into new objects, using your keys from the array.

提交回复
热议问题