[英]sorting keys of an association array
我有这种数据:
{
"Item 1": {
"Purchased": {
"quantity": "5.000",
"cost": "80.000"
},
"Rent": {
"quantity": "45.000",
"cost": "25200.000"
}
},
"Item 2": {
"Purchased": {
"quantity": "35.000",
"cost": "25000.000"
},
"Rent": {
"quantity": "0.0",
"cost": "0.0"
}
},
"Item 3": {
"Rent": {
"quantity": "25.000",
"cost": "50.000"
},
"Purchased": {
"quantity": "0.0",
"cost": "0.0"
}
},
"Item 4": {
"Rent": {
"quantity": "5.000",
"cost": "80.000"
},
"Purchased": {
"quantity": "0.0",
"cost": "0.0"
}
}
}
数据被打印到准备好的模板中。 撇开它,我想要的是将每个项目的“已购买”和“出租”部分按字母顺序排序。 这里的项目1和2是正确的,但项目3和4不是。 问题是来自服务器的项目可能带有不同种类的数据。 例如,第2项仅与Purchased一起出现,因此添加了0,0的Rent组件。
我在这里看到了一些字符串排序函数,但是它们是针对单个数组的,在这里我没有采用它们。
//因此,您的意思是您基本上希望将“已购买的商品”放在“出租”之前? 正如其他人所说,对象实际上并没有秩序。 因此,如果您想控制顺序,则可以使用lodash或类似方法将其映射到数组
_ = require('lodash');
_.map(properties, (property, index) => {
let thisProp = {};
thisProp[`Item ${index + 1}`] = [{Purchased: property.Purchased}, {Rent: property.Rent}];
return thisProp;
});
这将以以下形式返回数据:
[
{
"Item 1": [
{
"Purchased": {
"quantity": "5.000",
"cost": "80.000"
}
},
{
"Rent": {
"quantity": "45.000",
"cost": "25200.000"
}
}
]
},
{
"Item 2": [
{
"Purchased": {
"quantity": "35.000",
"cost": "25000.000"
}
},
{
"Rent": {
"quantity": "0.0",
"cost": "0.0"
}
}
]
}
] //etc.
您也可以执行@Jonny Rathbone建议的操作,而不必使用任何其他库。 做就是了
for (var p in DAT)
DAT[p]={Purchased:DAT[p].Purchased,
Rent:DAT[p].Rent};
JSON.stringify()现在以所需顺序列出属性。 但是,正如@xufox和@blex早已评论过的那样:JavaScript中属性的顺序尚未真正定义,因此,在此获得的结果可能并非在所有浏览器或JavaScript的未来版本中都是可持续的。
以下代码段在最受欢迎的浏览器中满足您的要求。 但是,正如您的问题下面已经提到的那样,规范并未强制对象保持其顺序。
const data = { "Item 1": { "Purchased": { "quantity": "5.000", "cost": "80.000" }, "Rent": { "quantity": "45.000", "cost": "25200.000" } }, "Item 2": { "Purchased": { "quantity": "35.000", "cost": "25000.000" }, "Rent": { "quantity": "0.0", "cost": "0.0" } }, "Item 3": { "Rent": { "quantity": "25.000", "cost": "50.000" }, "Purchased": { "quantity": "0.0", "cost": "0.0" } }, "Item 4": { "Rent": { "quantity": "5.000", "cost": "80.000" }, "Purchased": { "quantity": "0.0", "cost": "0.0" } } } function sortItem(item) { const sortedKeys = Object.keys(item).sort() return sortedKeys.reduce((accu, key) => ({ ...accu, [key]: item[key] }), {}) } const result = Object.keys(data).reduce((accu, key) => ({ ...accu, [key]: sortItem(data[key]) }), {}) console.log(result)
因此,我推荐以下使用数组的解决方案(始终保证结果的顺序):
const data = { "Item 1": { "Purchased": { "quantity": "5.000", "cost": "80.000" }, "Rent": { "quantity": "45.000", "cost": "25200.000" } }, "Item 2": { "Purchased": { "quantity": "35.000", "cost": "25000.000" }, "Rent": { "quantity": "0.0", "cost": "0.0" } }, "Item 3": { "Rent": { "quantity": "25.000", "cost": "50.000" }, "Purchased": { "quantity": "0.0", "cost": "0.0" } }, "Item 4": { "Rent": { "quantity": "5.000", "cost": "80.000" }, "Purchased": { "quantity": "0.0", "cost": "0.0" } } } function sortItem(item) { const sortedKeys = Object.keys(item).sort() return sortedKeys.reduce((accu, key) => [...accu, { key, ...item[key] }], []) } const sortedKeys = Object.keys(data).sort() const result = sortedKeys.reduce((accu, key) => [...accu, { key, info: sortItem(data[key]) }], []) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.