How does js insert an element into an array object?

before insertion:

var arr = [
    {
        id:1,
        name:""
    },
    {
        id:2,
        name:""
    }
]

after insertion:

var arr = [
    {
        id:1,
        name:"",
        age:18
    },
    {
        id:2,
        name:"",
        age:21
    }
]

how to achieve this with js?


/**
 * 
 * @param {Array} arr 
 */
function newArr(arr) {
  var arr = arr.slice(0); // 
  arr.forEach(function(val, ind) {
    val.age = parseInt(Math.random()*25); //  0-24  age
  })
  return arr;
}

var arr = [
  {
      id:1,
      name:""
  },
  {
      id:2,
      name:""
  }
];

console.log(newArr(arr));
< hr >

can also be changed to:

/**
 * 
 * @param {Array} arr 
 */
function newArr(arr) {
  return arr.map(function(val, ind) {
    return {
      id: val.id,
      name: val.name,
      age: parseInt(Math.random()*25) //  0-24  age
    }
  });
}

var arr = [
  {
      id:1,
      name:""
  },
  {
      id:2,
      name:""
  }
];

console.log(newArr(arr));

let newArr = arr.map(obj => {
    return {
        id: obj.id,
        name: obj.name,
        age: 18
    }
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bdc527-312e4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bdc527-312e4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?