gpt4 book ai didi

javascript - 如何在 JavaScript 中将 {"name":"myName","value":"myValue"} 对象的数组转换为 "myName":"myValue"对的对象?

转载 作者:行者123 更新时间:2023-12-02 18:26:52 25 4
gpt4 key购买 nike

我是 JavaScript 新手。我相信这对于有经验的人来说会很容易。

Amazon AWS SDK for Node.JS 的 SimpleDB getAttributes 函数返回如下数据数组:

my_JSON_object = {"Attributes":[{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"}]}

我想将它转换成一个简单的对象,如下所示:

my_JSON_object = {"Attributes":{"myName":"myValue","myName":"myValue",...}}

这样我就可以使用点语法来访问内容,例如:Attributes.myName 等。

如何进行这样的转换?

最佳答案

var my_JSON_object = {"Attributes":[{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"},{"Name":"myName","Value":"myValue"}]}

var my_new_JSON_object = { 'Attributes' : {} };

my_JSON_object.Attributes.forEach( function ( value, index ) {
my_new_JSON_object.Attributes[ value.Name ] = value.Value;
} );

关于javascript - 如何在 JavaScript 中将 {"name":"myName","value":"myValue"} 对象的数组转换为 "myName":"myValue"对的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18281980/

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