Let’s consider the next array:
const arr = [
{
name: "bob",
age: 25,
salary: 1000
},
{
name: "bill",
age: 32,
salary: 1500
},
{
name: "jake",
age: 16,
salary: null
},
]
I need to map every object to be the next structure:
firstName: string;
personAge: string;
grossSalary?: number;
so
const mappedArr = arr.map(person => ({
firstName: person.name,
personAge: person.age,
...{grossSalary:
person.salary
? person.salary
: // I'm stuck :'((
}
}))
I need to map person.salary
only if it’s not null
in the original object. Otherwise, I need to omit it.
I believe I’m pretty close with the spread operator but I guess I need a ternary to return an empty object if the salary is null
in the original object. Maybe this approach is wrong... idk anymore...