(javascript object), set same value to next value if greater than previous value, And versa

  Kiến thức lập trình

i have list of object ,

i want to compare value id to next value, if less than previous value, assign new key named IDS and set value to "1", why exactly (1), because its started from first object in array.

is same thing in case the next value is greater than previous value.

This is what I can explain.
For parts (4, 5) and (2 ,3) I find it difficult to explain this part.

and thank you in advance.

let lists = [
  { price:10, id:1},
  { price:11, id:2},
  { price:12, id:3},
  { price:13, id:4},
  { price:14, id:5},

  { price:12, id:4},//this part
  { price:12, id:5},//this part

  { price:11, id:4},
  { price:10, id:3},
  { price:9,  id:2},

  { price:12, id:3},//this part
  { price:12, id:2},//this part
]

Result:

let lists = [
  { price:10, id:1, IDS:1 },
  { price:11, id:2, IDS:1 },
  { price:12, id:3, IDS:1 },
  { price:13, id:4, IDS:1 },
  { price:14, id:5, IDS:1 },

  { price:12, id:4, IDS:2 },
  { price:12, id:5, IDS:2 },

  { price:11, id:4, IDS:3 },
  { price:10, id:3, IDS:3 },
  { price:9,  id:2, IDS:3 },

  { price:12, id:3, IDS:4 },
  { price:12, id:2, IDS:4 },
]

LEAVE A COMMENT