Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

compare salaries of employees and print salaries less than 5000

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}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading