I have an array of objects, called specials
, with each object looking like this:
{
apr_rate:"",
client_id:"",
disclaimer:",
lease_payment: "",
make:"",
model:"",
name:"",
platform:"",
price:"",
specialOrder:"",
specialStyle: "",
trim:"",
visible:"",
website:"",
year:"",
}
I have a loop going through each object in the array and checking to see if the lease_payment
property is empty. In the case that it is, I splice that particular object out of the array, store it in a new object called tempObj
, and then push it into a new array called tempArray
. The reason I am splicing is because I need to order this array of objects first by lease_payment
in ascending order - when there is no lease payment, it needs to order the remaining objects by price
in ascending order. However, the ordering of the price needs to happen after the ordering of lease. See below:
if (specials[i].lease_payment == "") {
tempObj = specials.splice(i, 1);
tempArray.push(tempObj);
} else {
// else just assume they're all lease offers and sort by lease payment
specials.sort(function(a, b) {
return a.lease_payment - b.lease_payment
});
}
I then check to see if that new array tempArray
has 1 object, or multiple. If there is only 1, I immediately push it back into the main specials
array, where it will be in the back; there is not another object to compare it too and order. If there is more than one object, I reorder those objects based on ascending price, followed by pushing them individually into the specials
array, since they need to be seen as there own objects. See below.
if (tempArray.length == 1) {
specials.push({tempArray});
} // else just sort the temp array by ascending price, push into main specials later
else if (tempArray.length > 1) {
tempArray.sort(function(a, b) {
return a.price - b.price
});
// grabs each individual object within temp array and pushes one at a time into main specials
for (i = 0; i < tempArray.length; i++) {
specials.push(tempArray[i]);
}
}
What is happening is that in either case, whenever I push an object back into the specials
array, it being nested within another array, as seen in this screenshot:
In this instance, two of the 5 specials objects were taken out, sorted, and put back in. However, they are now nested inside an array.
Am I missing something or doing something wrong? Any help would be appreciated.
It happens, because splice
returns an array. So in line
tempObj = specials.splice(i, 1);
tempObj
will be an array an stored a such. A solution would be, to write
tempObj = specials.splice(i, 1)[0];
Your push repush is a bit complex. I would reorder it like this:
specials.sort((a,b)=>{
if(a.lease_payment && b.lease_payment){
return a.lease_payment-b.lease_payment;//sort after lease payment
}else if(a.lease_payment){
return 1;//lease payment is already upper
}else if(b.lease_payment){
return -1;//swapp
}else{
//no leasepayment sort after prize;
return a.price - b.price;
}});
Or short:
specials.sort((a,b)=>a.lease_payment&&b.lease_payment?a.lease_payment-b.lease_payment:a.lease_payment?1:b.leasepayment?-1:a.price-b.price);
you make it more readable:
var leased = specials.filter(function(e) { return e.lease_payment != ""; }).sort(function(a, b) { return b.price - a.price });
var notLeased = specials.filter(function(e) { return e.lease_payment == ""; }).sort(function(a, b) { return b.price - a.price });
specials = leased.concat(notLeased);
You could use only a sort function, which sorts first by
lease_payment
, asending, assuming the value is a string price
, ascending, assuming value is a number var data = [{ lease_payment: '', price: 30 }, { lease_payment: '', price: 10 }, { lease_payment: 'a', price: 11 }, { lease_payment: 'b', price: 13 }, { lease_payment: 'c', price: 15 }, { lease_payment: '', price: 8 }]; data.sort(function (a, b) { return a.lease_payment.localeCompare(b.lease_payment) || a.price - b.price; }); console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.