Home > Mobile >  How can I push a prop and its value to objects that don't have that specific property knowing t
How can I push a prop and its value to objects that don't have that specific property knowing t

Time:07-14

what I want to achieve is:-

Loop through the peopleData array, add a prop call it 'age' and give it a value of '-' if one of the objects (array elements) does not have the key 'age'

const peopleData = [
  { name: "Ann", age: 15, email: "[email protected]", birthMonth: "Jan" },
  { name: "Bob", age: 19, email: "[email protected]", birthMonth: "Mar" },
  { name: "Cam", age: 18, email: "[email protected]", birthMonth: "Feb" },
  { name: "Dan" },
  { name: "Steve", birthMonth: "Jun" },
  { name: "Tyson", age: 20, birthMonth: "Dec" },
];

That what I tried to do and it did not work

const addAgeObj = {age: '-'}

const myArr = peopleData.map(personData=> {
  if(!personData.age) {
  peopleData.push(addAgeObj);
  console.log(peopleData);
}
});

CodePudding user response:

peopleData.push is wrong. You need to update the object, not to add new objects to the array.

const peopleData = [
  { name: "Ann", age: 15, email: "[email protected]", birthMonth: "Jan" },
  { name: "Bob", age: 19, email: "[email protected]", birthMonth: "Mar" },
  { name: "Cam", age: 18, email: "[email protected]", birthMonth: "Feb" },
  { name: "Dan" },
  { name: "Steve", birthMonth: "Jun" },
  { name: "Tyson", age: 20, birthMonth: "Dec" },
];

const myArr = peopleData.map(personData=> {
  return {...personData, age: personData.age || '-'}
});

console.log(myArr);

CodePudding user response:

const addAgeObj = {age: '-'}
const peopleData = [
    { name: "Ann", age: 15, email: "[email protected]", birthMonth: "Jan" },
    { name: "Bob", age: 19, email: "[email protected]", birthMonth: "Mar" },
    { name: "Cam", age: 18, email: "[email protected]", birthMonth: "Feb" },
    { name: "Dan" },
    { name: "Steve", birthMonth: "Jun" },
    { name: "Tyson", age: 20, birthMonth: "Dec" },
];
const myArr = peopleData.map(personData=> {
    /* if(!personData.age) {
    peopleData.push(addAgeObj);
    console.log(peopleData); 
    }*/
    if(!Object.keys(personData).includes("age"))
        personData={...personData,addAgeObj}
    return personData
});
console.log(myArr)

Try this

CodePudding user response:

peopleData.push does is adding new object and override the existing object.So that is wrong and that is not what you want.so try this one.

const peopleData = [
  { name: "Ann", age: 15, email: "[email protected]", birthMonth: "Jan" },
  { name: "Bob", age: 19, email: "[email protected]", birthMonth: "Mar" },
  { name: "Cam", age: 18, email: "[email protected]", birthMonth: "Feb" },
  { name: "Dan" },
  { name: "Steve", birthMonth: "Jun" },
  { name: "Tyson", age: 20, birthMonth: "Dec" },
];


const myArr = peopleData.map(personData=> {
  if(!personData.age) {
  
 let newobj={...personData,age:"-"};
  
 return newobj;
  
   }
else{

return personData;

  }
});

console.log(myArr);
  • Related