-1

Let’s say I Got an array of objects Like this:

let array = [
{Course: “A”, Week: “3”}, 
{Course: “B”, Week: “5”},
{Course: “”, Week: “”}, 
{Course: “”, Week: “”},
{Course: “”, Week: “”}, 
]

How Can I modify this array so the obejcts is place in the array based on the value of Week? E.g obejct with value 3 in Week Will be third element (index 2) in the array? In other Words swap place with the element on that index?

egx
  • 389
  • 2
  • 14
  • `Array.prototype.sort()` – Andreas Nov 30 '20 at 11:41
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Liam Nov 30 '20 at 11:42
  • This Will not place them on the index based on the value of Week? – egx Nov 30 '20 at 11:42

1 Answers1

0
let array = [{Course: “A”, Week: “3”}, 
{Course: “B”, Week: “5”},
{Course: “”, Week: “”}, 
{Course: “”, Week: “”},
{Course: “”, Week: “”}]   

const list = array.sort((a,b) => a.Week - b.Week);
Or Assayag
  • 5,662
  • 13
  • 57
  • 93