我开发了电子商务应用程序。
我有一份产品清单。
这是我的阵列:
[
{
Desc:Commodo adipisicing officia non excepteur officia nostrud,
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Commodo adipisicing officia non excepteur officia nostrud. Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 161.0,
}
},
{
Desc:Laborum cupidatat dolor tempor cillum incididunt,
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Laborum cupidatat dolor tempor cillum incididunt in veniam nisi dolore.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 80.0,
}
},
{
Desc:Anim ad et sunt aliquip proident,
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Anim ad et sunt aliquip proident fugiat deserunt.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 150.0,
}
},
{
Desc:Eiusmod aliquip eu aute elit eiusmod proident,
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Eiusmod aliquip eu aute elit eiusmod proident.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 130.0,
}
}
]
我需要根据PRICE_SALE
进行排序。
Low to High and high to low.
我试过了:
var a=this.product.sort(function(a, b){
console.log("SORT");
return a.details.PRICE_SALE-b.details.PRICE_SALE;
})
但这不起作用。
答案 0 :(得分:2)
将数组元素转换为对象:
var arr = [
{
"Desc" : "Commodo adipisicing officia non excepteur officia nostrud",
"details" :{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Commodo adipisicing officia non excepteur officia nostrud. Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 161.0,
}
},
{
"Desc": "Laborum cupidatat dolor tempor cillum incididunt",
"details":{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Laborum cupidatat dolor tempor cillum incididunt in veniam nisi dolore.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 80.0,
}
},
{
"Desc" : "Anim ad et sunt aliquip proident",
"details":{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Anim ad et sunt aliquip proident fugiat deserunt.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 150.0,
}
},
{
"Desc":"Eiusmod aliquip eu aute elit eiusmod proident",
"details":{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Eiusmod aliquip eu aute elit eiusmod proident.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 130.0,
}
}
];
查看结果:
var a = arr.sort(function(a, b){
return a.details.PRICE_SALE - b.details.PRICE_SALE;
})
a.forEach(function(ele) {
console.log(ele.details.PRICE_SALE); // 80, 130, 150, 161
})
答案 1 :(得分:1)
如果您修复了Desc
上的引用问题,那么您的代码运行正常。请注意,sort()
方法将数组排序。它还返回已排序的数组,但是不要让它让你误以为它会创建数组的副本并对其进行排序(例如.sort()
不能像array.slice()
那样完全复制数组或部分并返回副本)。
var products = [
{
Desc:"Commodo adipisicing officia non excepteur officia nostrud",
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Commodo adipisicing officia non excepteur officia nostrud. Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 161.0,
}
},
{
Desc:"Laborum cupidatat dolor tempor cillum incididunt",
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Laborum cupidatat dolor tempor cillum incididunt in veniam nisi dolore.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 80.0,
}
},
{
Desc:"Anim ad et sunt aliquip proident",
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Anim ad et sunt aliquip proident fugiat deserunt.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 150.0,
}
},
{
Desc:"Eiusmod aliquip eu aute elit eiusmod proident",
details:{
"P_TITLE" : "Martina",
"P_TYPE" : "simple",
"P_DESCRIPTION" : "Eiusmod aliquip eu aute elit eiusmod proident.Eu voluptate amet elit nulla.",
"P_STOCK" : 100.0,
"PRICE_REGULAR" : 194.0,
"PRICE_SALE" : 130.0,
}
}
];
products.sort( function( a, b ) {
return a.details.PRICE_SALE - b.details.PRICE_SALE;
});
console.log( JSON.stringify( products, null, 4 ) );