[英]I want to sort an array of objects in JavaScript with Two properties Boolean value and Int
我想在JavaScript中使用两个属性布尔值和Int值对对象数组进行排序。我希望输出如下:
{
first_name: 'sumeet',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 40
}, {
first_name: 'Pirate',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 50
}, {
first_name: 'Lazslo',
last_name: 'Jamf',
offerApplicable: 'TRUE',
price: 60
}, {
first_name: 'jitendra',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 101
}
具有真值的所有对象首先也按价格排序,然后所有具有布尔值的对象为false。 我能够按布尔值对它们进行排序,但我也希望按价格对它们进行排序。
我试过这个
var objs = [
{ first_name: 'Lazslo', last_name: 'Jamf' ,offerApplicable: 'TRUE' ,price: 60 },
{ first_name: 'Pig', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 100},
{ first_name: 'Pirate', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 50},
{ first_name: 'nithesh', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 40},
{ first_name: 'sumeet', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 40},
{ first_name: 'mahesh', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 40},
{ first_name: 'jitendra', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 101}
];
function compare(a,b) {
var aConcat = a["offerApplicable"] + a["price"];
var bConcat = b["offerApplicable"] + b["price"];
if (aConcat < bConcat )
return 1;
if (aConcat > bConcat )
return -1;
return 0;
}
console.log(objs.sort(compare));
我得到这样的输出
first_name:“Lazslo”last_name:“Jamf”offerApplicable:“TRUE”价格:60
:first_name:“Pirate”last_name:“Prentice”offerApplicable:“TRUE”价格:50
first_name:“sumeet”last_name:“Prentice”offerApplicable:“TRUE”价格:40
first_name:“jitendra”last_name:“Prentice”offerApplicable:“TRUE”价格:101
first_name:“nithesh”last_name:“Bodine”offerApplicable:“FALSE”价格:40
first_name:“mahesh”last_name:“Bodine”offerApplicable:“FALSE”价格:40
first_name:“Pig”last_name:“Bodine”offerApplicable:“FALSE”价格:100
任何帮助thanx提前。
只需多做几步即可。
首先,获取不同数组中的所有true和false组件。
像这样
const trueEntities = defaultArray.filter(e => e.offerApplicable === 'TRUE');
const falseEntities = defaultArray.filter(e => e.offerApplicable === 'FALSE');
然后按价格排序这个数组
const sortedByPriceTrue = trueEntities.sort((a, b) => a.price - b.price);
const sortedByPriceFalse = falseEntities.sort((a, b) => a.price - b.price);
然后简单地返回/使用变量并行数组
return sortedByPriceTrue.concat(sortedbyPriceFalse);
我知道这个解决方案可能比一个操作慢,但更具可读性
ps编辑,第一个解决方案也没有用
objs.sort(function (a, b) {
return (a.price - b.price);
});
objs.sort(function (a, b) {
var price1 = (a.offerApplicable === 'TRUE');
var price2 = (b.offerApplicable === 'TRUE');
return price2 - price1;
});
首先,我们按价格对值进行排序,然后按offerApplicable字段对值进行排序。
综合解决方案
objs.sort(function (a, b) {
var price1 = (a.offerApplicable === 'TRUE');
var price2 = (b.offerApplicable === 'TRUE');
return price2 - price1 || a.price - b.price;
});
您可以链接Array#sort
的sort函数的rewturn值的条件。
第一部分
(b.offerApplicable === 'TRUE') - (a.offerApplicable === 'TRUE')
如果offerApplicable
值为'TRUE'
则执行检查。 与true
的比较将项目移动到底部,因此我们需要用a
和b
切换支票。
第二部分
a.price - b.price
仅在先前的delta为零时进行评估,然后返回价格的delta。
var array = [{ first_name: 'Lazslo', last_name: 'Jamf', offerApplicable: 'TRUE', price: 60 }, { first_name: 'Pig', last_name: 'Bodine', offerApplicable: 'FALSE', price: 100 }, { first_name: 'Pirate', last_name: 'Prentice', offerApplicable: 'TRUE', price: 50 }, { first_name: 'nithesh', last_name: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_name: 'sumeet', last_name: 'Prentice', offerApplicable: 'TRUE', price: 40 }, { first_name: 'mahesh', last_name: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_name: 'jitendra', last_name: 'Prentice', offerApplicable: 'TRUE', price: 101 }]; array.sort(function (a, b) { return (b.offerApplicable === 'TRUE') - (a.offerApplicable === 'TRUE') || a.price - b.price; }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.