gpt4 book ai didi

javascript - 如何将一个json数组插入到一个json中

转载 作者:行者123 更新时间:2023-11-30 09:11:24 25 4
gpt4 key购买 nike

我有一个json数组-

var finalResponse2 = [
{Transaction Amount: {type: "number"}},
{UTR number: {type: "string"}}
]

我想这样转换

responses : [
{
Transaction Amount: {type: "number"},
UTR number: {type: "string"}
}]

我写的代码是这样的-

     var innerField1 =  {};
var innerField2 = {};
for (var l=0 ; l < this.finalResponse2.length ; l++){

let key1 = Object.keys(this.finalResponse2[l])[0]
let value1 = Object.values(this.finalResponse2[l])[0]
let key2 = Object.keys(value1)[0]
let value2 = Object.values(value1)[0]


innerField1[key2] = value2
key1[key2] = value2

key1.key2 = value2
fields['responses'].key1.key1[key2] = value2
}

}

But I am getting error - Cannot create property 'type' on string 'Transaction Amount'

最佳答案

您可以使用 Object.assign 函数和扩展运算符 ... 来获得所需的结果:

let finalResponse2 = [
{"Transaction Amount": {type: "number"}},
{"UTR number": {type: "string"}}
];

let result = [Object.assign({}, ...finalResponse2)];
console.log(result);

关于javascript - 如何将一个json数组插入到一个json中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58660726/

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