'Search an array list inside another array and return a new object combined
I am using typescript and I have two objects:
export interface Module {
moduleId: String
name: String
}
export interface Model {
id: number
name: String
modules: Module[]
}
I have the following list:
[
{
"id": 1,
"name": "Object number one",
"modules": [
{
"moduleId": 101,
"name": "Módulo 101"
},
{
"moduleId": 102,
"name": "Módulo 102"
}
]
},
{
"id": 2,
"name": "Object number two",
"modules": [
{
"moduleId": 201,
"name": "Módulo 201"
}
]
},
{
"id": 3,
"name": "Object number theree",
"modules": [
{
"moduleId": 301,
"name": "Módulo 301"
},
{
"moduleId": 302,
"name": "Módulo 302"
},
{
"moduleId": 303,
"name": "Módulo 303"
}
]
},
{
"id": 4,
"name": "Object number four",
"modules": [
{
"moduleId": 401,
"name": "Módulo 402"
}
]
}
]
I have to create a function which will receive a module list and I want to add a lambda function (using filter
, find
, and so on) to get a new object made up of question and answer.
Example:
Parameters:
{
102,
201,
303
}
This would be my response:
[
{
"model": {
"id": 1,
"name": "Object number one"
},
"module": {
"moduleId": 102,
"name": "Módulo 102"
}
},
{
"model": {
"id": 2,
"name": "Object number two"
},
"module": {
"moduleId": 201,
"name": "Módulo 201"
}
},
{
"model": {
"id": 3,
"name": "Object number theree"
},
"module": {
"moduleId": 303,
"name": "Módulo 303"
}
}
]
I want to use a lambda expression or a one-line function:
function doWork(modulesId: String[]){
const result : list.filter(x =>)...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|