gpt4 book ai didi

javascript - 如何将两个 json 属性合并到一个数组中

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

我有一个像这样的json数据:

var data = [{
"RM_Name": "Cloud Strife",
"Division": "Avalanche",
"RM_Phone": "No Phone",
"RC_Name": "No RM",
"RC_Phone": "No Phone",
"States_Covered": "Midgar,Nibelheim"
}, {
"RM_Name": "Clint Eastwood",
"Division": "Hollywood",
"RM_Phone": "No Phone",
"RC_Name": "No RC",
"RC_Phone": "No Phone",
"States_Covered": "Gran Torino"
}, {
"RM_Name": "Barret Wallace",
"Division": "Avalanche",
"RM_Phone": "No Phone",
"RC_Name": "Barry Allen",
"RC_Phone": "No Phone",
"States_Covered": "North Corel"
}, {
"RM_Name": "Tifa Lockheart",
"Division": "Avalanche",
"RM_Phone": "No Phone",
"RC_Name": "Aeris Gainsborough",
"RC_Phone": "No Phone",
"States_Covered": "Sector 7"
}];

现在我想创建一个由 RM_Name 和 RC_Name 组成的新数组,如下所示,输出如下:

[Cloud Strife,Clint Eastwood,Barret Wallace,Barry Allen,Tifa Lockheart,Aeris Gainsborough]

我怎样才能做到这一点?我尝试过使用$.merge()。但我认为输出是错误的。非常感谢任何帮助。

最佳答案

使用 jQuery .each()迭代 JSON。试试这个:

var dataArray = [];
$.each(data, function(i, v){
dataArray.push(v.RM_Name);
if(v.RC_Name !="No RC")
dataArray.push(v.RC_Name);
});
console.log(dataArray);

DEMO FIDDLE

关于javascript - 如何将两个 json 属性合并到一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491104/

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