array projection and js objects

array projection and js objects

by Fernando De Leon

HTML

<button id="button">
click me
</button>
<button id="button2">
See the array
</button>
<button id="button3">
Lets play with the js object notation
</button>
<button id="button4">
Check if object is copy else build it
</button>

JavaScript

var employees = { person: []
 };
 
 var students = { person:[]};
 
 var aPersonObj = { name:"nando",school:[
   									{kindy:"abc"},{kindy:"bb1"}
 									]};
 var copyPersonObj;

$("#button3").on("click",function() {
console.log(aPersonObj);
 console.log(aPersonObj.name);
 aPersonObj.school.forEach(function(elem,index) {
   console.log(elem.kindy + " <-- at index:"+index);
 });
 copyPersonObj = aPersonObj;

});

$("#button4").on("click",function() {
 console.log(copyPersonObj);
 if(copyPersonObj === undefined) {
  copyPersonObj = { name:"",school:[]};
 }
 console.log("After we have initialise it: "+copyPersonObj);
 copyPersonObj.school.forEach(function(elem,index) {
  console.log(elem.kindy + " <--- copy obj at index :"+index);
 });

});


 
 
 
$("#button").on("click",function() {
 employees.person.push({
  'firstName':"Nando",
  'lastName':'De Leon',
  'age':49
   
 });
 employees.person.push({
  'firstName':"Bob",
  'lastName':'Gon',
  'age':50
   
 });
 
 console.log("Do we have students: "+students.person.length);
 console.log("the employess is "+employees.person.length);
 employees.person.forEach(function(value,index) {
  console.log(value.firstName+ " index: "+index);
 });
 var firstElement = employees.person[0].firstName;
 console.log("the first element is:"+firstElement);

});

$("#button2").on("click",function() {
 employees.person.forEach(function(value,index) {
  console.log(value.firstName + " "+value.lastName);
 });
});