Here I have attendance details and I Want to filter every data that contains employees id:1
.
for example: I have data like this:
const attendance = [
{
date: 1,
employees: [
{
id: 1,
name: 'mahadev',
status: 'p'
},
{
id: 2,
name: 'roshan',
status: 'p'
},
]
},
{
date: 2,
employees: [
{
id: 1,
name: 'mahadev',
status: 'a'
},
{
id: 2,
name: 'roshan',
status: 'p'
},
]
},
];
And I want Output like this:
[
{
date:1,
employees: [
{
id:1,
name:'mahadev',
status:'p'
}
]
},
{
date:2,
employees: [
{
id:1,
name:'mahadev',
status:'a'
}
]
},
]
CodePudding user response:
const attendance = [{
date: 1,
employees: [
{ id: 1, name: 'mahadev', status: 'p' },
{ id: 2, name: 'roshan', status: 'p' }
]
},
{
date: 2,
employees: [
{ id: 1, name: 'mahadev', status: 'a' },
{ id: 2, name: 'roshan', status: 'p' }
]
},
];
const filtered = id =>
attendance.map(a => {
const employees = a.employees.filter(e => e.id === id);
return { ...a, employees };
});
console.log(filtered(1));
CodePudding user response:
Using map()
and filter()
const filtered = id =>
attendance.map(a => {
const employees = a.employees.filter(emp => emp.id === id);
return { date:a.date, employees };
});
console.log(filtered(1))