如何通过此函数从键=>值对中获取值

时间:2018-08-23 10:38:04

标签: jquery

我正在使用

将Json对象转换为数组
var ticks= {firstkey: "firstvalue", secondkey: "secondvalue", thirdkey: "thirdValue"}

    var arr = $.map(ticks, function(value, index) {
                            return [[index,value]];
                        });

将输出作为

  

0:[“ firstkey”,“ firstvalue”] 1:[“ secondkey”,“ secondvalue”]   2:[““ thirdkey”,“ thirdValue”]

我想要的结果

  

FirstKey:FirstValue SecondKey:SecondValue ThirdKey:ThirdValue

关联数组形式

1 个答案:

答案 0 :(得分:0)

您可以遍历对象并相应地推送到数组。希望这会有所帮助

var ticks= {firstkey: "firstvalue", secondkey: "secondvalue", thirdkey: "thirdValue"};

var arr = Array();

for (var tick in ticks) {
arr.push({[tick]:ticks[tick]});
}

console.log(arr[0]);
console.log(arr[2].thirdkey);