05-11-2020 8:20 AM
var myobjs=[{
name:mohan,
age:28,
},
{name:ram,
age:28,
dept:csc},
{name:jhon,
age:28,
dept:mech}];
how can i insert dept in the first index of the object in java script
i want output like this
{name:mohan,
age:28,
dept:csc
},
{name:ram,
age:28,
dept:csc},
{name:jhon,
age:28,
dept:mech};
05-11-2020 8:33 AM
Hi,
Try using the unshift method like so: your_array.unshift( new_element_of_the_array );
regards,
Mateusz
05-11-2020 8:38 AM
Hi
You can do something like this.
(you may correct the syntax)
var original = {
name: "abc",
age: "25"
};
var final = {
name: "abc",
value: "25"
dept : "xxx"
};
for (let i = 0; i < original.length; i++) {
final.name[i] = original.name[i]
final.age[i] = original.age[i]
if original.dept[i] is ''
{ .....}
}
Regards,
Venkat
05-11-2020 10:28 AM
JavaScript allows you to just simply add the property to the object:
var arr = [{
name: "Alice",
age: 24
}, {
name: "Alice",
age: 24,
height: 175
}]
console.log(arr[0].height) // This will log undefined
arr[0].height = 180
console.log(arr[0].height) // This will log 180