0

now I have an object like let x = {}; what I wanna do is to be able to result in something like

{'1':[obj,obj,obj], '2':[obj,obj]}

so what I do is x[id] = [obj] cause here I receive an object but I wanna produce an array of objects with concat to result like the example I mentioned. SO, here is the problem it overrides the array of objects every time how I can concat here ??

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Jack
  • 43
  • 3

1 Answers1

1

Spread the current values and add the new obj

x[id] = [...x[id],obj]
Alexander
  • 3,019
  • 1
  • 20
  • 24
  • 2
    This is [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax), not destructuring. And, this will throw an error initially when there is no `id` key – adiga Jun 08 '21 at 12:36
  • Correct, OP should check that. – Alexander Jun 08 '21 at 12:46