I have an object in JavaScript:
var obj = {
"a": "test1",
"b": "test2"
}
How do I check that te
var obj = {"a": "test1", "b": "test2"};
var getValuesOfObject = Object.values(obj)
for(index = 0; index < getValuesOfObject.length; index++){
return Boolean(getValuesOfObject[index] === "test1")
}
The Object.values() method returned an array (assigned to getValuesOfObject) containing the given object's (obj) own enumerable property values. The array was iterated using the for
loop to retrieve each value (values in the getValuesfromObject) and returns a Boolean() function to find out if the expression ("text1" is a value in the looping array) is true.
you can try this one
var obj = {
"a": "test1",
"b": "test2"
};
const findSpecificStr = (obj, str) => {
return Object.values(obj).includes(str);
}
findSpecificStr(obj, 'test1');
getValue = function (object, key) {
return key.split(".").reduce(function (obj, val) {
return (typeof obj == "undefined" || obj === null || obj === "") ? obj : (_.isString(obj[val]) ? obj[val].trim() : obj[val]);}, object);
};
var obj = {
"a": "test1",
"b": "test2"
};
Function called:
getValue(obj, "a");
You can try this:
function checkIfExistingValue(obj, key, value) {
return obj.hasOwnProperty(key) && obj[key] === value;
}
var test = [{name : "jack", sex: F}, {name: "joe", sex: M}]
console.log(test.some(function(person) { return checkIfExistingValue(person, "name", "jack"); }));
You can use Object.values():
The
Object.values()
method returns an array of a given object's own enumerable property values, in the same order as that provided by afor...in
loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
and then use the indexOf() method:
The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
For example:
Object.values(obj).indexOf("test`") >= 0
A more verbose example is below:
var obj = {
"a": "test1",
"b": "test2"
}
console.log(Object.values(obj).indexOf("test1")); // 0
console.log(Object.values(obj).indexOf("test2")); // 1
console.log(Object.values(obj).indexOf("test1") >= 0); // true
console.log(Object.values(obj).indexOf("test2") >= 0); // true
console.log(Object.values(obj).indexOf("test10")); // -1
console.log(Object.values(obj).indexOf("test10") >= 0); // false
This should be a simple check.
Example 1
var myObj = {"a": "test1"}
if(myObj.a == "test1") {
alert("test1 exists!");
}