I have array of object arrobj and arrlist prgList,
I need to fetch the name from the arrobj based on the programs in the list using javascript.
If the program prg exists in the arrobj , fetch and should display as show in expectd output
tried
var lists=[]
var result =arrobj.find(e=> e.prg.includes(prgList) ? lists.push(e))); // bit stuck
var arrobj=[
{id:1, name: "user1", prg: ["onex", "twox", "threex"]},
{id:2, name: "user2", prg: ["onex", "threex"]},
{id:3, name: "user3", prg: ["twox", "threex"]}
]
var prgList =["onex", "twox", "threex"]
Expected Output:
{
onex: ["user1", "user2"],
twox: ["user1", "user3"],
threex: ["user1", "user2", "user3"]
}
2
Answers
You can do something like this:
You can do for instance as following:
From each element in
prgList
create a[key, value]
pair, where the element is thekey
and thevalue
is the list of users, which contain the key, mapped to their names only.