Context: I have a form where a user enters in data. I want to remove all object elements in the phone array that contain an empty string in the phoneNumber property.
Here is example data:
Here is the solution I have come up with but when I console.log(newData), I get undefined.
Any clue on what I'm doing wrong?
Here is example data:
JavaScript:
const data =
[
{
contactName: "Contact 1 Name",
phone: [
{phoneName: "Some Name", phoneNumber: "(111) 111-1111"}
{phoneName: "Some Name2", phoneNumber: "(222) 222-2222"}
{phoneName: "Some Name2", phoneNumber: ""}
]
},
{
contactName: "Contact 2 Name",
phone: [
{phoneName: "Some Name", phoneNumber: "(333) 333-3333"}
{phoneName: "Some Name2", phoneNumber: "(444) 444-4444"}
{phoneName: "Some Name2", phoneNumber: ""}
]
}
]
Here is the solution I have come up with but when I console.log(newData), I get undefined.
JavaScript:
const newData = data.
.map((array) => array)
.forEach((contactObject) => {
return contactObject.phone.filter(
(phoneArray) => phoneArray.phoneNumber !== ""
);
});
console.log("newData: ", newData);
Any clue on what I'm doing wrong?
Last edited: