[英]How to sort Json file based on value of object?
I have below json file named all, and contain below data我有以下名为 all 的 json 文件,并包含以下数据
[{"name":{"common":"Dominican Republic","official":"Dominican Republic","nativeName":{"spa":{"official":"República Dominicana","common":"República Dominicana"}}},"region":"Americas","area":48671.0,"flags":{"png":"https://flagcdn.com/w320/do.png","svg":"https://flagcdn.com/do.svg"}},{"name":{"common":"Heard Island and McDonald Islands","official":"Heard Island and McDonald Islands","nativeName":{"eng":{"official":"Heard Island and McDonald Islands","common":"Heard Island and McDonald Islands"}}},"region":"Antarctic","area":412.0,"flags":{"png":"https://flagcdn.com/w320/hm.png","svg":"https://flagcdn.com/hm.svg"}},{"name":{"common":"Ghana","official":"Republic of Ghana","nativeName":{"eng":{"official":"Republic of Ghana","common":"Ghana"}}},"region":"Americas","area":238533.0,"flags":{"png":"https://flagcdn.com/w320/gh.png","svg":"https://flagcdn.com/gh.svg"}}]
I want to sort this file based on name.official value, I wrote below codes but it does not work.我想根据 name.official 值对这个文件进行排序,我写了下面的代码,但它不起作用。 how can I achieve that?
我怎样才能做到这一点?
const path = "../all.json;
getCuntries(path);
async function getCuntries(path) {
const response = await fetch(path);
const scrampled = await response.json();
const parsed = await JSON.parse(JSON.stringify(scrampled));
const data = parsed.sort(GetSortOrder("name"));
console.log(data)
}
function GetSortOrder(prop) {
return function(a, b) {
if (a[prop][1] > b[prop][1]) {
return 1;
} else if (a[prop][1] < b[prop][1]) {
return -1;
}
return 0;
}
}
Your comparator method is a bit messed up.您的比较器方法有点混乱。 First, comparing strings with > and < operators may lead to unexpected behaviour sometimes and this is not the recommended way to compare strings.
首先,使用 > 和 < 运算符比较字符串有时可能会导致意外行为,这不是比较字符串的推荐方法。 And second (which is probably the root cause), you should access object values with their keys and not with indexes (like you're doing with
[1]
).其次(这可能是根本原因),您应该使用它们的键而不是索引来访问对象值(就像您使用
[1]
所做的那样)。 This should work - make it the body of function (a, b)
:这应该有效 - 使其成为
function (a, b)
的主体:
const aPassedProp = a[prop];
const bPassedProp = b[prop];
return aPassedProp.official.localeCompare(bPassedProp.official);
The localeCompare method can compare your strings in an appropriate way: you can learn more here . localeCompare 方法可以以适当的方式比较您的字符串:您可以在此处了解更多信息。
You can't access an object key by index.您不能通过索引访问对象键。 As it's is not specified if the object key should be dynamic, here a simple solution where it's not:
由于没有指定对象键是否应该是动态的,所以这里有一个简单的解决方案:
const data = [ { "name": { "official": "Republic of Ghana"} }, { "name": { "official": "Heard Island and McDonald Islands" } }, { "name": { "official": "Dominican Republic" } }, ]; // shortened for readability const sortArray = (a, b) => { const aCompare = a.name.official; const bCompare = b.name.official; if (aCompare === bCompare) { return 0; } return aCompare < bCompare ? -1 : 1; }; data.sort(sortArray); console.log(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.