×

edutechional's video: JS Tutorial: Find if Two Object Values are Equal to Each Other

@JS Tutorial: Find if Two Object Values are Equal to Each Other
In this JavaScript programming tutorial, we are going to walk through how we can build out a function to test to see if the values of two objects are equal or if they're not. Now, you may think that this doesn't actually need a function. Because if you're coming from another programming language or just from a pure common stance standpoint, what we're going to walkthrough seems like it should happen automatically. But this is one of the more tricky parts of JavaScript. And so, I'm going to walk through first why this type of process is needed. So I'm going to create a couple objects. So I'm going to say, "const obj1" and then this object is going to have a name property. And then let's also give it an age. Now, if I copy this and simply name it something different, we can see that these values are completely identical. Every part of them. The data type, the number keys, and their values are identical. So you would think that if you did something like this where you said, "obj1 === obj2," you would think that this would come and say that these items are equal. But they're not. And if you think that, okay, maybe this is one of those problems where you need to only use the double equals for equivalents, because remember, in JavaScript, three equals means full complete equivalents, and the two is a little bit of a lighter equivalents, this is still false. And so, the reason for this is because when you compare objects, the way that JavaScript looks at them is it takes the object as a whole. It actually ignores the value when it comes to equivalents. It simply looks at these objects, and it says, "Okay, this is object one. We have it stored in one spot in memory, and it's name is obj1." Now we have object two, this is stored in a different place in memory, and it has a different variable name. So there's no way of looking at these and saying that they are the same thing. And so this can be a very tricky kind of concept, and the reason why I'm creating this video is because later on I'm going to be creating another video that needs to know if ... And it's a part of a larger algorithm and a coding interview exercise. And you need to know if two objects have the same values or not. And so, we need to build a function that tests for that, and if you have two objects like this, it needs to say that they're true. So that's what we're going to build out in this guide. Now the first thing I'm going to do is I'm actually going to call the function that we're going to be building so that we can debug it as we're creating it. So I'm going to call this function isEqual, and it's going to take two arguments. It's going to take the object one and object two. Right now, it's giving an error obviously because we haven't created the function. Let's come up to the very top of the file and let's start doing that. So I'm going to give us some space right here and I'm going to create an arrow function. So I'll say, "const isEqual" and this is going to take the two arguments. So we'll just call them object one and object two. You can name these anything you want, but I think that's nice and descriptive, and this will be an arrow function. And now, the very first thing I want to test for here, or I should say the first thing that I want to grab, is the set of keys. So inside the object, we have these keys of name and of age. So let's grab those for each one of the objects. So I'm going to say, "const obj1Keys," and we'll set the sequel to the object class .keys and then pass in object one. And then we're going to do the same thing or object two, and if you want to see what these values are, you can see right there. We get an arrayvec for name and age, and we know that both of these are identical. We're getting the same name and the same age, because we have two objects that are passing in right now that have the same keys. Now, if you were to change one of these, so instead of age you were to say something like height, now you can see that object two now is showing height instead of age. So that's what we're looking for right there. And let me get rid of the debugging output. Hopefully this js tutorial is helping you to learn JavaScript. Written Guide and Code: https://www.edutechional.com/2018/09/28/how-to-check-if-two-javascript-objects-have-the-same-values/ Follow me: Twitter: https://twitter.com/jordanhudgens Instagram: https://www.instagram.com/jordanhudgens/ GitHub: http://github.com/jordanhudgens

500

51
edutechional
Subscribers
42.4K
Total Post
685
Total Views
1.2M
Avg. Views
19K
View Profile
This video was published on 2018-09-29 00:33:53 GMT by @edutechional on Youtube. edutechional has total 42.4K subscribers on Youtube and has a total of 685 video.This video has received 500 Likes which are higher than the average likes that edutechional gets . @edutechional receives an average views of 19K per video on Youtube.This video has received 51 comments which are higher than the average comments that edutechional gets . Overall the views for this video was lower than the average for the profile.

Other post by @edutechional