Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 15, 2023 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/b9f7c85ccd21c3e2bd186abcc963b797 to your computer and use it in GitHub Desktop.
Save code-boxx/b9f7c85ccd21c3e2bd186abcc963b797 to your computer and use it in GitHub Desktop.
Check If Value Exists In Javascript Object

JAVASCRIPT CHECK VALUE EXISTS IN OBJECT

https://code-boxx.com/check-value-exists-in-object-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Check Value Exist In Object</title>
<script>
// (A) THE OBJECT
var obj = {
Name: "Jon Doe",
Email: "jon@doe.com",
Gender: "Male"
};
// (B) EXTRACT ALL VALUES INTO ARRAY
var val = Object.values(obj);
console.log(val); // ["Jon Doe", "jon@doe.com", "Male"]
// (C) CHECK USING ARRAY FUNCTIONS
console.log(val.includes("Jon Doe")); // true
console.log(val.indexOf("Jon Doe")); // 0, will be -1 if not found
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Check Value Exist In Object</title>
<script>
// (A) THE OBJECT
var obj = {
Name: "Jon Doe",
Email: "jon@doe.com",
Gender: "Male"
};
// (B) MANUALLY LOOP & SEARCH
var check = "Jon Doe";
var found = false;
for (let key in obj) { if (obj[key] == check) {
found = true;
break;
}}
console.log(found); // true
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Check Value Exist In Object</title>
<script>
// (A) THE OBJECT
var obj = {
Name: "Jon Doe",
Email: "jon@doe.com",
Gender: "Male",
Numbers: [12, 34],
Fav: {
Colors: ["Red", "Green", "Blue"],
Foods: ["Pizza", "Spam"]
}
};
/*
// (B) THIS WILL NOT WORK!
// values() WILL ONLY RETURN FIRST LEVEL
var val = Object.values(obj);
console.log(val); // ["Jon Doe", "jon@doe.com", "Male", OBJECT]
console.log(val.includes("Pizza")); // false
*/
// (C) WE NEED A RECURSIVE FUNCTION TO DIG DEEP
var flatten = value => {
// (C1) COLLECT ALL VALUES IN TEMP
let temp = [];
// (C2) ARRAY OR OBJECT
if (typeof value == "object") {
if (Array.isArray(value)) {
for (let i of value) { temp = temp.concat(flatten(i)); }
} else {
for (let i in value) { temp = temp.concat(flatten(value[i])); }
}
}
// (C3) FLAT STRING, NUMBER, OR BOOLEAN
else { temp.push(value); }
// (C4) FLATTENED VALUE
return temp;
};
// (D) GO!
var flat = flatten(obj);
console.log(flat); // all values in flat array
console.log(flat.includes("Pizza")); // true
console.log(flat.indexOf("Pizza")); // 8
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment