根据angularjs中的另一个数组结果创建具有键值的数组

时间:2018-06-08 03:36:18

标签: javascript angularjs

我有一个像这样的数组,

0:{tag_20: "2018051607234047", tag_12: "700", tag_20_credit_n: "529010804376-S"}

基于上面的数组我想创建像这样的新数组

0:{code: "tag_20", property: "2018051607234047"} 1:{code: "tag_12", property: "700"} 2:{code: "tag_20_credit_n", property: "529010804376-S"}

我是新的这个关键价值概念和 angularjs 到目前为止我尝试过这个

var log = []; angular.forEach(values, function(value, key) { this.push('code: ' + key + ' property: ' + value); }, log); 

但是收到错误。

1 个答案:

答案 0 :(得分:1)

您可以获取对象数组并将对象映射为具有所需键值对的新数组。



var array = [{ tag_20: "2018051607234047", tag_12: "700", tag_20_credit_n: "529010804376-S" }],
    result = array.map(o => Object
        .entries(o)
        .map(([code, property]) => ({ code, property }))
    );

console.log(result);