Praveen Kumar Praveen Kumar - 2 months ago 5
AngularJS Question

How to add one more element into all array field?

var employees = [

{
name: "Praveen",
dateofbirth: new Date("July 24, 1992"),
gender: "Male",
salary: 12000,
city: "Salem"
}, {
name: "Divya",
dateofbirth: new Date("July 21, 1994"),
gender: "Female",
salary: 13500.33,
city: "Thirupur"
}, {
name: "Elangovan",
dateofbirth: new Date("May 5, 1989"),
gender: "Male",
salary: 14050.45,
city: "Namakkal"
}, {
name: "Abinaya",
dateofbirth: new Date("August 21, 1991"),
gender: "Female",
salary: 15230.78,
city: "Thirupur"
}, {
name: "Surendran",
dateofbirth: new Date("July 3, 1993"),
gender: "Male",
salary: 16302.44,
city: "vellur"
}, {
name: "Hindhuja",
dateofbirth: new Date("November 05, 1995"),
gender: "Female",
salary: 17000.11,
city: "Salem"
}
]


This my code!!, My question is " How to add "SPOC :987654321" into all field?

Answer

You could use Array#forEach, then apply just the property with the value.

var employees = [{ name: "Praveen", dateofbirth: new Date("July 24, 1992"), gender: "Male", salary: 12000, city: "Salem" }, { name: "Divya", dateofbirth: new Date("July 21, 1994"), gender: "Female", salary: 13500.33, city: "Thirupur" }, { name: "Elangovan", dateofbirth: new Date("May 5, 1989"), gender: "Male", salary: 14050.45, city: "Namakkal" }, { name: "Abinaya", dateofbirth: new Date("August 21, 1991"), gender: "Female", salary: 15230.78, city: "Thirupur" }, { name: "Surendran", dateofbirth: new Date("July 3, 1993"), gender: "Male", salary: 16302.44, city: "vellur" }, { name: "Hindhuja", dateofbirth: new Date("November 05, 1995"), gender: "Female", salary: 17000.11, city: "Salem" }];

employees.forEach(function (a) {
    a.SPOC = 987654321;
});

console.log(employees);
.as-console-wrapper { max-height: 100% !important; top: 0; }