I have the following JSON structure.
comapany : XYZ
Emp Info:[
{
empName: A,
empID:1
salary[
{
year: 2017,
value: 3000
},
{
year: 2018,
value: 5000
}
]
},
{
empName: B,
empID:2
salary[
{
year: 2017,
value: 6000
},
{
year: 2018,
value: 3000
}
]
},
{
empName: C,
empID:3
salary[
{
year: 2017,
value: 3000
},
{
year: 2018,
value: 8000
}
]
},
]
Here Emp Info is an array and inside this contains another array of salaries. I want to filter all the salaries which have value of 3000. So my output should look like below
comapany : XYZ
Emp Info:[
{
empName: A,
empID:1
salary[
{
year: 2017,
value: 3000
}
]
},
{
empName: B,
empID:2
salary[
{
year: 2018,
value: 3000
}
]
},
{
empName: C,
empID:3
salary[
{
year: 2017,
value: 3000
}
]
},
]
I have tried multiple things using lodash, but it always returns all the data(and not filtering).
._filter(data, data => {
return data.empInfo.salary[0].value === 3000
});
._each(data.empInfo.salary, data => {
_.filter(data, d=>{
return d.salary === 3000;
});
});
._filter(data.empInfo.salary, data => {
_.each(salary, s => {
return s === 3000;
});
});
Can someone help in filtering this json preferably using lodash
let newEmpInfo = empInfo.map( info => {
return { ... info, salary: info.salary.filter( salary => salary.value === 3000 )};
});
That's the easiest way, no need for lodash. Simple array operations in JS.
As a side note, you should add your coding attempts next time, as usually in SO this kinds of questions where people just come here so we can solve their problems are un-welcomed and usually gets a lot of downvotes.