Edit**** I believe I messed up the explanation originally. Thank you to Brilliand for pointing that out.
Right now I can map through the data, fine. I want to reorganize it, so I can map and display it by the highest sale of the day to lowest.
data[0].stats.one_day_sales = 2
data[1].stats.one_day_sales = 17
data[2].stats.one_day_sales = 10
The output I am looking for, mind you each [i] contains other data that I want to go along with the reorganization.
data[1].stats.one_day_sales = 17
data[2].stats.one_day_sales = 10
data[0].stats.one_day_sales = 2
I have attached a photo of the data I am receiving as well to better clarify.
Thanks for your help in advance!
Not sure what you are going at, I put your sample data into one before proceeding to sort. What you are looking for is probably the sort function. I sorted them ascending in number but if you want them descending just swap the a with b:
const array1 = [{ stats: { one_day_sales: 7 } }]
const array2 = [{ stats: { one_day_sales: 4 } }]
const array3 = [{ stats: { one_day_sales: 2 } }]
let arrayOfObjects = [array1[0], array2[0], array3[0]]
arrayOfObjects.sort((a, b) => {
// one day sales being the identifier for sorting
return a.stats.one_day_sales - b.stats.one_day_sales // swap a with b for descending
})
console.log(arrayOfObjects)
You could destructure the array and assign the new array in wanted order.
let array1 = [{ stats: { one_day_sales: 7 }}],
array2 = [{ stats: { one_day_sales: 4 }}],
array3 = [{ stats: { one_day_sales: 2 }}];
[array1, array2, array3] = [array1, array2, array3]
.sort(([a], [b]) => b.stats.one_day_sales - a.stats.one_day_sales);
console.log(array1);
console.log(array2);
console.log(array3);
.as-console-wrapper { max-height: 100% !important; top: 0; }