From e0de0dd6ae6d0105d5df0aa1384ca006c790b4d6 Mon Sep 17 00:00:00 2001 From: Andrej Rasevic <andrej@rasevicengineering.com> Date: Thu, 10 Jun 2021 12:13:43 -0400 Subject: [PATCH] adding sample code from lecture --- CodeExamples/Week2/jsObjectComparison.js | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 CodeExamples/Week2/jsObjectComparison.js diff --git a/CodeExamples/Week2/jsObjectComparison.js b/CodeExamples/Week2/jsObjectComparison.js new file mode 100644 index 0000000..28906c9 --- /dev/null +++ b/CodeExamples/Week2/jsObjectComparison.js @@ -0,0 +1,31 @@ +let team1 = { + teamName: 'The Bullets', + sport: 'Basketball', + city: 'Washington' +} + +let team2 = { + teamName: 'The Bullets', + sport: 'basketball', + city: 'Washington' +} + +let team3 = team1 + +function shallowObjectComparison(obj1, obj2) { + let keys1 = Object.keys(obj1) + let keys2 = Object.keys(obj2) + if (keys1.length !== keys2.length) { + return false + } + // there are the same number of keys in each array, i.e. the same number of properties in each object + for (let key of keys1) { + if (obj1[key] !== obj2[key]) { + return false + } + } + return true +} +console.log(team3 === team1) +console.log(team2 === team1) +console.log(shallowObjectComparison(team1, team2)) \ No newline at end of file -- GitLab