gpt4 book ai didi

javascript - 将javascript对象转换为有序的逗号分隔值

转载 作者:行者123 更新时间:2023-11-29 15:45:50 24 4
gpt4 key购买 nike

我试图让json中的项目有序排列。我能够选择 json 中存在的“术语”值,但是否可以按照我在预期输出部分中显示的方式进行安排?我添加了一个 jsfiddle 链接以显示我到达的位置:

[
{
"Link": "http://testLink.com/1",
"_index": "test",
"_source": {
"Author": "SAM",
"Map": [
{
"Company": [
{
"Apple_Inc": [
{
"count": 1,
"term": "Apple"
}
],
"sector": "Technology",
"term": "Apple Inc",
"ticker": "AAPL",
"type": "BCap"
}
],
"count": 1,
"term": "Company"
},
{
"Country": [
{
"Canada": [
{
"Canada": [
{
"count": 1,
"term": "Toronto"
}
],
"count": 1,
"term": "Canada"
}
],
"United_States": [
{
"count": 1,
"term": "United States"
}
],
"currency": "Dollar (USD)",
"index": "DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX",
"region": "North Americas",
"term": "Canada"
}
],
"count": 1,
"term": "Country"
},
{
"Personality": [
{
"count": 1,
"term": "Bart Prince"
},
{
"count": 1,
"term": "Thomas"
},
{
"count": 1,
"term": "Deborah Hornstra"
},
{
"count": 1,
"term": "Henderson Sotheby"
},
{
"count": 1,
"term": "Max Alliance"
}
],
"count": 5,
"term": "Personality"
}
]
},
"id": "YMFT112"
},
{
"Link": "http://testLink.com/2",
"_id": "YMFT113",
"_index": "test",
"_source": {
"Author": "MAX",
"Map": [
{
"Company": [
{
"Microsoft Corp": [
{
"count": 1,
"term": "Microsoft"
}
],
"sector": "Technology",
"term": "Microsoft",
"ticker": "AAPL",
"type": "BCap"
}
],
"count": 1,
"term": "Company"
},
{
"Country": [
{
"Brazil": [
{
"count": 1,
"term": "Brazil"
}
],
"currency": "Dollar (USD)",
"region": "South Americas",
"term": "Brazil"
}
],
"count": 1,
"term": "Country"
},
{
"SalesRelated": [
{
"count": 1,
"term": "traffic"
}
]
},
{
"Personality": [
{
"count": 1,
"term": "Maximor"
},
{
"count": 1,
"term": "R.V.P"
},
{
"count": 1,
"term": "Wenger"
},
{
"count": 1,
"term": "SAF"
}
],
"count": 4,
"term": "Personality"
}
]
}
}
]

http://jsbin.com/exuwet/3/edit


提示输入如果字段 Selected = Country,

预期输出:

YMFT112;    Country;    United States;  United States;      NA;         http://testLink.com/1;

YMFT112; Country; Canada; Canada; Toronto; http://testLink.com/1;

YMFT113; Country; Brazil; Brazil; NA; http://testLink.com/2;

如果字段 Selected = Company,

预期输出:

YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1;

YMFT113; Company; Microsoft Corp; Microsoft; http://testLink.com/2;

最佳答案

您可以在 native 可用时使用 JSON 对象或使用 JSON2作为垫片。

之后,只需使用 JavaScript 的内置排序功能即可。您提供一个函数来比较数组项

var myArray = JSON.parse(jsonString);
myArray.sort(function(a, b){
var nameA = a._source.Map.Company.term;
var nameB = b._source.Map.Company.term;

if (nameA === nameB) {
return 0;
} else if (nameA < nameB) {
return -1
}
return 1;
});

关于javascript - 将javascript对象转换为有序的逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452106/

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