Below is the pseudo-code of the function, followed by the code snippet:
function hasSameData if both objects have the same number of keys (properties) if every key of obj1 matches with the corresponding key of obj2 and values of the same keys of both objects match. return true return false
- We will find keys of both objects by Object.keys(), which returns an array of keys of that object.
- For checking that every key in obj1 is also present in obj2, and if values of those key matches, we will use every() method. The every() method accepts a callback and returns “true” or “false” according to the callback condition.
Note: The above approach does not work for nested objects (Objects and arrays inside an
object). In such cases, it needs function according to the nested object.
Applying the above function would fail in case of nested objects as shown below:
- Extract unique objects by attribute from array of objects.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : prerak_jain