You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Musical Album Collection Object Manipulation Example
varcollection={"2548": {"album": "Slippery When Wet","artist": "Bon Jovi","tracks": ["Let It Rock","You Give Love a Bad Name"]},"2468": {"album": "1999","artist": "Prince","tracks": ["1999","Little Red Corvette"]},"1245": {"artist": "Robert Palmer","tracks": []},"5439": {"album": "ABBA Gold"}};varcollectionCopy=JSON.parse(JSON.stringify(collection));functionupdateRecords(id,prop,value){if(prop==="tracks"&&value!==""){if(collection[id][prop]){collection[id][prop].push(value);// if the track array exist, add the new value at the last element.}else{collection[id][prop]=[value];// if the track array is not set, add the new value as a array.}}elseif(value!==""){collection[id][prop]=value;// if the value is not empty, add the value to the corresponding prop.}else{deletecollection[id][prop];// if value is empty, delete the given prop.}returncollection;}// Alter values below to test your codeupdateRecords(5439,"artist","ABBA");updateRecords(5439,"tracks","Take a Chance on Me");updateRecords(2548,"artist","");updateRecords(1245,"tracks","Addicted to Love");updateRecords(2468,"tracks","Free");updateRecords(2548,"tracks","");updateRecords(1245,"album","Riptide");
Profile data lookup Example
varcontacts=[{"firstName": "Akira","lastName": "Laine","number": "0543236543","likes": ["Pizza","Coding","Brownie Points"]},{"firstName": "Harry","lastName": "Potter","number": "0994372684","likes": ["Hogwarts","Magic","Hagrid"]},{"firstName": "Sherlock","lastName": "Holmes","number": "0487345643","likes": ["Intriguing Cases","Violin"]},{"firstName": "Kristian","lastName": "Vos","number": "unknown","likes": ["JavaScript","Gaming","Foxes"]}];functionlookUpProfile(name,prop){for(vari=0;i<contacts.length;i++){if(contacts[i].firstName==name){if(contacts[i].hasOwnProperty(prop)){returncontacts[i][prop];}else{return"No such property";}}}return"No such contact";}// Change these values to test your functionlookUpProfile("Akira","likes");lookUpProfile("Kristian","lastName");lookUpProfile("Sherlock","likes");lookUpProfile("Harry","likes");lookUpProfile("Bob","number");lookUpProfile("Bob","potato");lookUpProfile("Akira","address");