-4

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!

Arrays of Objects

JWADE77
  • 5
  • 1
  • To sort those variables, you're gonna have to combine them into one variable. Why are they separate anyway? – Brilliand Dec 10 '21 at 20:45
  • They are individual query results that have been pushed together – JWADE77 Dec 10 '21 at 20:47
  • please add the wanted result. – Nina Scholz Dec 10 '21 at 20:48
  • It sounds like you'd be better off concatenating them together, and using them as `array[0]`, `array[1]`, etc. instead of `array1`, `array2`, etc. Sorting would then be straightforward. – Brilliand Dec 10 '21 at 20:49
  • I believe that is how it is right now for example to retrieve data I would do data[i].stats.one_day_sales (excuse me if I am wrong) – JWADE77 Dec 10 '21 at 20:52
  • You edited your question, but what you did is resort them with the same name, that is impossible since you can't just swap them in the code. is your goal to have array 1 to always the the one with the highest number of sales? A much easier way would be to store your arrays in another array, sort that array and access them via array[0] being the highest number and so son – Branchverse Dec 10 '21 at 20:55
  • Based on the photo, apparently you're already receiving the data in array form, and your whole `array1`, `array2` bit is just misleading? In that case you can just use `data.sort`, simple. – Brilliand Dec 10 '21 at 20:58
  • yes, I believe I messed the question up, I will reorganize it, I have also tried data. sort earlier to no avail. – JWADE77 Dec 10 '21 at 21:02
  • still unclear, what you want. – Nina Scholz Dec 10 '21 at 21:16

2 Answers2

1

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)
Branchverse
  • 1,203
  • 1
  • 7
  • 19
1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392