-4

Hi can anyone tell how to exactly use Flat or flatMap method to flat this employee data array, so that I can get the slaries which are less than 5000, with the name of the employees.

const employeeData = [{
    name: "Suresh",
    salary: 7500
}, {
    name: "Aman",
    salary: 3400
}, {
    name: "Munish",
    salary: 10000
}, {
    name: "Chad",
    salary: 12000
}, {
    name: "Naveen",
    salary: 9500
}];

const flattendEmpdata = employeeData.flat((name, salary) => [name, employeeData[salary]]);

I was trying to use Flat and filter method in chaining method, the desired ouput will be like this

{name: Suresh, salary: 7500} , {name: Aman, salary: 3400}

1 Answers1

0

You're looking for the filter method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

const employeeData = [{
    name: "Suresh",
    salary: 7500
}, {
    name: "Aman",
    salary: 3400
}, {
    name: "Munish",
    salary: 10000
}, {
    name: "Chad",
    salary: 12000
}, {
    name: "Naveen",
    salary: 9500
}];

const flattendEmpdata = employeeData.filter(input => input.salary < 5000);
console.log(flattendEmpdata);
Bender
  • 103
  • 8