gpt4 book ai didi

Javascript 访问名称/值对

转载 作者:行者123 更新时间:2023-12-04 20:05:19 26 4
gpt4 key购买 nike

我得到如下所示的 JSON 名称/值对:

{
"Name":"parentid",
"Value":"blah"
},
{
"Name":"siteid",
"Value":"blah"
},
{
"Name":"sitename",
"Value":"blah"
}

但我想将“name”值作为 KEY,将“value”值作为 VALUE。有没有一种优雅的方法可以将那段 JSON 变成这样的东西?

{'parentid', 'blah'},
{'sitename', 'blah'}

最佳答案

试试这个:

var items = [
{
"Name":"parentid",
"Value":"blah"
},
{
"Name":"siteid",
"Value":"blah"
},
{
"Name":"sitename",
"Value":"blah"
}
];

var results = new Object();

for (var i = 0; i < items.length; i++)
{
results[items[i].Name] = items[i].Value;
}

这将导致类似:

var results = { parentid: "Blah", siteid: "Blah", sitename: "Blah" };

关于Javascript 访问名称/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1925069/

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