0

i have these 3 array

[
  { animals: 'dog', user_id: 12, y: 1 },
  { animals: 'cat', user_id: 1, y: 3 }
]
[
  { Habitation: 'Appartement',  user_id: 1, y: 1 },
  { Habitation: 'Mansion', user_id: 12, y: 1 },
  { xHabitation: 'House', user_id: 1, y: 2 }
]
[
  { device : 'computer', user_id: 1, y: 1 },
  { device : 'laptop', user_id: 1, y: 2 },
  { device : 'tablet', user_id: 12, y: 1 }
]

So these 3 array have something in common : user_id

And i want to create something like this

[
  1 = {
      pets : [{ x: 'cat', y: 3 }],
      house: [{ x: 'Appartement', y: 1 },  { x: 'House', y: 2 }],
      device: [{ x: 'computer', y: 1 },   { x: 'laptop', y: 2 }]
    },
  12 = {
     pets : [{ x: 'dog', y: 1 }],
     house: [{ x: 'Mansion', y: 1 }],
     device: [{ x: 'tablet', y: 1 }]
   }
]

As you can see object are created based on user_id, i dont see/know how to perform a clean code for that in native javascript

Thanks

taranticoscr
  • 35
  • 1
  • 6
  • Create the new array/object for the users (your output is invalid, if you want the user_id as key, you need an object instead). Then iterate over your input arrays, extract the user_id and add to the user object if it exists, otherwise create it first. How to restructure data is asked here all the time, there are tons of examples to find. Also please add an attempt at doing this yourself, this isn't a free code writing service. –  Jun 02 '22 at 09:43
  • How you get categories `pets`, `house` and `device`? It is associated to property `y`? – Yosvel Quintero Jun 02 '22 at 10:34
  • @yosvel yes, "y" is number of cat for example – taranticoscr Jun 02 '22 at 10:42
  • Please, share the categories data with their respective `number` – Yosvel Quintero Jun 02 '22 at 12:40
  • @YosvelQuinteroArguelles the first 3 array are just sql result, "y" are the count of pets column that has "cat" for exemple in row – taranticoscr Jun 02 '22 at 14:51
  • I see, but need a way to relate `pets` to -> `['cat', 'dog']` as well as `house` to -> `['Appartment', 'House', 'Mansion']` and `device` to -> `['computer', 'laptop', 'tablet']` – Yosvel Quintero Jun 03 '22 at 06:20

0 Answers0