debugger;
//default person object.
var defperson = {"age":20,"company":"Holograph","pet":{"name":"mushy"}};
//array of names
var names = ["ben","bert","fred"]
//empty array to be pushed into
var people = [];
//for loop set to run through 3 iterations.
for(var x=0;x<3;x++){
//empty object for the iterations to be pushed into.
var person = {};
//p for property is each key, which outputs the value of that key, iterated through 3 times
for(var p in defperson){
//the defpersons properties are now being stored into the person object this is also iterated through 3 times as its within the for loop, really helps to usae debugger to understand the process.
person[p] = defperson[p];
}
//adding the names from the array into the person object
person.name = names[x];
//trying to add a pet name won't work as its an object within an object which is trying to be accessed by a value.
person.pet.name = "animal belongs to " + person.name;
//push the three person objects into the empty people array.
people.push(person);
}
//log the people array which contains 3 person objects with their properties.
console.log( people );
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.