0

I'm completely new to Typescript (and javascript) and working with a key-value pair object.

Say I have state.itemsInGroup[numbers]

I have the following object which puts itemId 255 at position 1, itemId 254 at position 2, and itemId 251 at position 3.

{"1":255,"2":254,"3":251}

I need to put itemId 248 in at position 3 and increase the total number of "numbers" to 4, so that the object becomes:

{"1":255,"2":254,"3":248,"4":251}

How best to achieve this?

Object.values(state.itemsInGroup[numbers]).splice("3", 0, "248") does not change anything and still returns {"1":255,"2":254,"3":251} ???

state.itemsInGroup[numbers]["3"] = "248" replaces the value {"1":255,"2":254,"3":248} but I want it inserted and the existing value moved to "4".

Thinking about it I probably need an iteration to increase the key of 251 from "3" to "4" right? Go easy on me I don't really know what I'm doing lol.

melable
  • 21
  • 3
  • key-value objects - or maps - don't have an order. You can just change the value for key "3" by assigning it and adding a new entry with the key "4". – mohlendo Sep 08 '22 at 08:59

0 Answers0