0

How to easily insert new property in a Map object at specific position(index).

For example I have Map object with more than 1000 key-value pairs and I want to one more at position 235.

object type Map

{
 1a001: {...},
 1a002: {...},
 1a003: {...},
 <--- insert item at position n
 ... 1000 of items
 1a1001: {...},
 1a1002: {...},
}

Something like

mapObject.insert({ ...someobject}, property, index)
rendom
  • 3,417
  • 6
  • 38
  • 49
  • 1
    The keys of a Map are in the order in which they’re inserted. It is not possible to insert a key in a specific position. – fubar May 08 '22 at 14:48
  • An array would be the better data structure for this usecase. – urchmaney May 08 '22 at 14:50
  • @urchmaney I need the "keyed" structure for quick data access. – rendom May 08 '22 at 14:58
  • 1
    I can see that the keys follow the same structure 1a{index}. I don't know if this format will be same through out the lifetime of your application, but this can help pull the data from the array structure. – urchmaney May 08 '22 at 15:02
  • Maps are not an indexed data structure. They do keep insertion order, but that doesn't mean they're meant for keeping ordered data or being reorderable. You cannot access them by index, you cannot insert by index. Use an array. If you also need keyed access, use both an array and a map. – Bergi May 08 '22 at 15:19

0 Answers0