October 22, 2024
Chicago 12, Melborne City, USA
javascript

Return an array of arrays, by filtering through an array of objects


Let’s say I have three categories for which I need to pull out the values.

const users = ["user1","user2","user 3"];
const ageGroup = ["millenial","gen z","gen a"];
const subjects = ["physics","history","math"];

The main object structure is

const userInfo = [
{"name":"user1", "gender":"female","ageGroup":"millenial", "subjects":["physics","history"], "value": 0.3
},
{"name":"user1", "gender":"female","ageGroup":"millenial", "subjects":["math","history"], "value": 0.5
},
{"name":"user2", "gender":"male","ageGroup":"gen z", "subjects":["physics","history"], "value": 0.3
},
{"name":"user2", "gender":"male","ageGroup":"gen z", "subjects":["physics","math"], "value": 0.7
},
{"name":"user3", "gender":"male","ageGroup":"gen a", "subjects":["history","math"], "value": 0.4
}
];

Now I want to I take a category variable (users/ageGroup/subjects), and it should, in the same order of the variable output the value in an array of arrays.
If I pass users, the expected output is:

[
[0.3,0.5],//user1
[0.3,0.7],//user2
[0.4]//user3
]

The order cannot be user 2, and then user 1.

I think this would work best as a function, but I’m open to simple operations as well.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video