gpt4 book ai didi

javascript - 更改 JavaScript 数据对象的格式

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:12 26 4
gpt4 key购买 nike

我有一个数据对象如下:

[{"Positive":"14.71","Neutral":"50.0","Negative":"35.29"}]

我想将其更改为以下指定格式:

[{"type":"Positive","value":14.71},{"type":"Neutral","value":50.0},{"type":"Negative","value":35.29}]

最佳答案

使用 Object.keys Array#map 方法。

var data = [{
"Positive": "14.71",
"Neutral": "50.0",
"Negative": "35.29"
}];

var res = Object.keys(data[0]) // get all object property names
// iterate over property names array
.map(function(k) {
// generate array element using property value
return {
type: k,
// cast data value to number using plus-sign prefix(as per requirement)
value: +data[0][k]
}
})

console.log(res);

关于javascript - 更改 JavaScript 数据对象的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39509495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com