我正在获取这个Json数组,我必须像我提到的数组那样对该数组进行排序,请帮助我使用javascript sort函数来获取该数组。
[{"key":"-Lp64sK3aNJ_K-jvGneA","value":"India"},
{"key":"-Lp64uAVsWCNFkRgPYnK","value":"China"},
{"key":"-LpjaFeusQm-9gkisTe4","value":"Switzerland"},
{"key":"-Lpjaj6nOSseZTF29ucJ","value":"Japan"},
{"key":"-LpjbqOF1tL1EFpiR4G0","value":"Iran"},
{"key":"-Lp6SJO-lp9mxt2IPSS9","value":"India,Andhra Pradesh"},
{"key":"-Lpv7SOKdlEkYlvIi36I","value":"India,Uttar Pradesh"},
{"key":"-Lp66I5deHmTf6WaU6WF","value":"China,Anhui"},
{"key":"-Lp66dTu7gwLBTjaqSkJ","value":"China,Fujian"},
{"key":"-LpvTcrZmubSqFuBvSMm","value":"India,Andhra Pradesh,Kanpur"},
{"key":"-Lpv7YKViplh9kBVKb-O","value":"India,Uttar Pradesh,Ghaziaabad"},{"key":"-LpvKlcP1EuJsSHEx9yH","value":"India,Uttar Pradesh,Agra"},
{"key":"-LpvTjo5OR8jUniZPuHh","value":"India,Uttar Pradesh,Kanpur"},
{"key":"-LpvUeEVFQu4raqmR1O3","value":"India,Uttar Pradesh,Gonda"},
{"key":"-LpvV7-2QouzF-Eil0_r","value":"India,Uttar Pradesh,Lucknow"},
{"key":"-LpwkUtmojicCEtUtEg4","value":"India,Uttar Pradesh,Bareily"},
{"key":"-LpwkUtshSmYbY_RV2F6","value":"India,Uttar Pradesh,Banaras"},
{"key":"-Lp68tYZvlwNWl8F6WSa","value":"China,Anhui,Anquing"},
{"key":"-Lp6TaKmOOyaIhk01J8_","value":"China,Anhui,Beijing"},
{"key":"-Lp6TlAJXHWkEqwdzwl5","value":"China,Anhui,Bengbu"},
{"key":"-Lq_xAkDuu7q5nS7YyCo","value":"China,Anhui,Chonnquing"},
{"key":"-Lp6z8Dut9vwRZuxoVr_","value":"China,Fujian,Fuzhou"}]
我希望使用JavaScript的排序功能将Above数组排序为below数组
================================================ ======================
[{"key":"-Lp64uAVsWCNFkRgPYnK","value":"China"},
{"key":"-Lp66I5deHmTf6WaU6WF","value":"Anhui,China"},
{"key":"-Lp68tYZvlwNWl8F6WSa","value":"Anquing,Anhui,China"},
{"key":"-Lp6TaKmOOyaIhk01J8_","value":"Beijing,Anhui,China"},
{"key":"-Lp6TlAJXHWkEqwdzwl5","value":"Bengbu,Anhui,China"},
{"key":"-Lq_xAkDuu7q5nS7YyCo","value":"Chonnquing,Anhui,China"},
{"key":"-Lp66dTu7gwLBTjaqSkJ","value":"Fujian,China"},
{"key":"-Lp6z8Dut9vwRZuxoVr_","value":"Fuzhou,Fujian,China"},
{"key":"-Lp64sK3aNJ_K-jvGneA","value":"India"},
{"key":"-Lp6SJO-lp9mxt2IPSS9","value":"Andhra Pradesh,India"},
{"key":"-LpvTcrZmubSqFuBvSMm","value":"Kanpur,Andhra Pradesh,India"},{
"key":"-Lpv7SOKdlEkYlvIi36I","value":"Uttar Pradesh,India"},
{"key":"-LpvKlcP1EuJsSHEx9yH","value":"Agra,Uttar Pradesh,India"},
{"key":"-LpwkUtshSmYbY_RV2F6","value":"Banaras,Uttar Pradesh,India"},
{"key":"-LpwkUtmojicCEtUtEg4","value":"Bareily,Uttar Pradesh,India"},
{"key":"-Lpv7YKViplh9kBVKb-O","value":"Ghaziaabad,Uttar Pradesh,India"},
{"key":"-LpvUeEVFQu4raqmR1O3","value":"Gonda,Uttar Pradesh,India"},
{"key":"-LpvTjo5OR8jUniZPuHh","value":"Kanpur,Uttar Pradesh,India"},
{"key":"-LpvV7-2QouzF-Eil0_r","value":"Lucknow,Uttar Pradesh,India"},
{"key":"-LpjbqOF1tL1EFpiR4G0","value":"Iran"},
{"key":"-Lpjaj6nOSseZTF29ucJ","value":"Japan"},
{"key":"-LpjaFeusQm-9gkisTe4","value":"Switzerland"}]
答案 0 :(得分:0)
您可以拆分值,将其取反并加入一个字符串,然后映射一个新对象。
var array = [{ key: "-Lp64uAVsWCNFkRgPYnK", value: "China" }, { key: "-Lp66I5deHmTf6WaU6WF", value: "China, Anhui" }, { key: "-Lp68tYZvlwNWl8F6WSa", value: "China, Anhui, Anquing" }, { key: "-Lp6TaKmOOyaIhk01J8_", value: "China, Anhui, Beijing" }, { key: "-Lp6TlAJXHWkEqwdzwl5", value: "China, Anhui, Bengbu" }, { key: "-Lq_xAkDuu7q5nS7YyCo", value: "China, Anhui, Chonnquing" }, { key: "-Lp66dTu7gwLBTjaqSkJ", value: "China, Fujian" }, { key: "-Lp6z8Dut9vwRZuxoVr_", value: "China, Fujian, Fuzhou" }, { key: "-Lp64sK3aNJ_K-jvGneA", value: "India" }, { key: "-Lp6SJO-lp9mxt2IPSS9", value: "India, Andhra Pradesh" }],
result = array.map(o => ({ ...o, value: o.value.split(/, \s*/).reverse().join(', ') }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
您可以创建国家/地区订单映射器并使用该映射器进行排序。
然后迭代以颠倒country state city
的顺序。
let arr = [
{ key: "-Lp64sK3aNJ_K-jvGneA", value: "India" },
{ key: "-Lp64uAVsWCNFkRgPYnK", value: "China" },
{ key: "-LpjaFeusQm-9gkisTe4", value: "Switzerland" },
{ key: "-Lpjaj6nOSseZTF29ucJ", value: "Japan" },
{ key: "-LpjbqOF1tL1EFpiR4G0", value: "Iran" },
{ key: "-Lp6SJO-lp9mxt2IPSS9", value: "India,Andhra Pradesh" },
{ key: "-Lpv7SOKdlEkYlvIi36I", value: "India,Uttar Pradesh" },
{ key: "-Lp66I5deHmTf6WaU6WF", value: "China,Anhui" },
{ key: "-Lp66dTu7gwLBTjaqSkJ", value: "China,Fujian" },
{ key: "-LpvTcrZmubSqFuBvSMm", value: "India,Andhra Pradesh,Kanpur" },
{ key: "-Lpv7YKViplh9kBVKb-O", value: "India,Uttar Pradesh,Ghaziaabad" },
{ key: "-LpvKlcP1EuJsSHEx9yH", value: "India,Uttar Pradesh,Agra" },
{ key: "-LpvTjo5OR8jUniZPuHh", value: "India,Uttar Pradesh,Kanpur" },
{ key: "-LpvUeEVFQu4raqmR1O3", value: "India,Uttar Pradesh,Gonda" },
{ key: "-LpvV7-2QouzF-Eil0_r", value: "India,Uttar Pradesh,Lucknow" },
{ key: "-LpwkUtmojicCEtUtEg4", value: "India,Uttar Pradesh,Bareily" },
{ key: "-LpwkUtshSmYbY_RV2F6", value: "India,Uttar Pradesh,Banaras" },
{ key: "-Lp68tYZvlwNWl8F6WSa", value: "China,Anhui,Anquing" },
{ key: "-Lp6TaKmOOyaIhk01J8_", value: "China,Anhui,Beijing" },
{ key: "-Lp6TlAJXHWkEqwdzwl5", value: "China,Anhui,Bengbu" },
{ key: "-Lq_xAkDuu7q5nS7YyCo", value: "China,Anhui,Chonnquing" },
{ key: "-Lp6z8Dut9vwRZuxoVr_", value: "China,Fujian,Fuzhou" }
];
let sortMap = { China: 0, India: 1, Iran: 2, Japan: 3, Switzerland: 4 };
arr.sort(
(a, b) => sortMap[a.value.split(",")[0]] - sortMap[b.value.split(",")[0]]
).forEach(e => e.value = e.value.split(',').reverse().join());
console.log(arr)