JS Loops
by ryan fosse
JavaScript
object_array = [
{id: 09876, first_name: "Jonathon", last_name: 'Mascorella', post_code: '2750', of_age: true},
{id: 09845, first_name: "James", last_name: 'Dean', post_code: '2750', of_age: true},
{id: 09829, first_name: "Keith", last_name: 'Licht', post_code: '2750', of_age: true},
{id: 09810, first_name: "Fran", last_name: 'Dilt', post_code: '2750', of_age: true}
];
//Adding someone to the array
new_user = {id: 09879, first_name: "Chris", last_name: 'Fentinal', post_code: '2750', of_age: true};
is_in_array = false;
for (i=0; i<object_array.length; i++){
if(object_array[i].id == new_user.id){
is_in_array = true;
alert("User is in Array!")
}
}
//add them if they are not in there
if(!is_in_array){
object_array.push(new_user);
console.log(object_array);
}
object_array.pop();
console.log(object_array);