1

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'
      }       
    ]  
  },
]

2 Answers 2

5

Try using map and filter.

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));

Sign up to request clarification or add additional context in comments.

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.