I have an array of JSON objects imdb
and I want to check if a key exists. I have tried couple different methods but none of them shows the correct result. I looked into this post but doesn't help. Below code
var imdb = [{"123":"hi"}, {"234":"hello"}]; //array of JSON object
var valEventTime = 123; //key I want to find if exists
//approach 1
function getValueByKey(key, data) {
var i, len = data.length;
for (i = 0; i < len; i++) {
if (data[i] && data[i].hasOwnProperty(key)) {
return data[i][key];
}
}
return -1;
}
if(getValueByKey(valEventTime, imdb) > -1){
console.log("Yes");
}
else {
console.log("NOT")
}
//approach 2
if (imdb[valEventTime]) {
console.log("Yes");
} else {
console.log("NOT")
}
//approach 3
var keys=Object.keys(imdb)
for(var i=0;i<keys.length;i++){
if(keys[i]==valEventTime)
{//check your key here
console.log("Yes")
}
else console.log("NOT")
}
The output always shows NOT
even though I am searching for a key that already exists (123
). Please suggest.
I have an array of JSON objects imdb
and I want to check if a key exists. I have tried couple different methods but none of them shows the correct result. I looked into this post but doesn't help. Below code
var imdb = [{"123":"hi"}, {"234":"hello"}]; //array of JSON object
var valEventTime = 123; //key I want to find if exists
//approach 1
function getValueByKey(key, data) {
var i, len = data.length;
for (i = 0; i < len; i++) {
if (data[i] && data[i].hasOwnProperty(key)) {
return data[i][key];
}
}
return -1;
}
if(getValueByKey(valEventTime, imdb) > -1){
console.log("Yes");
}
else {
console.log("NOT")
}
//approach 2
if (imdb[valEventTime]) {
console.log("Yes");
} else {
console.log("NOT")
}
//approach 3
var keys=Object.keys(imdb)
for(var i=0;i<keys.length;i++){
if(keys[i]==valEventTime)
{//check your key here
console.log("Yes")
}
else console.log("NOT")
}
The output always shows NOT
even though I am searching for a key that already exists (123
). Please suggest.
- can you update json data in your question ? – Chandru Commented Oct 16, 2017 at 6:13
- @Chandru not sure what you meant? I already have the JSON data var imdb = [{"123":"hi"}, {"234":"hello"}]; – nad Commented Oct 16, 2017 at 6:14
2 Answers
Reset to default 3The problem with your solution is that you are trying to search for a integer key where as your json key is a string.
var imdb = [{"123":"hi"}, {"234":"hello"}]; // key is string
var valEventTime = 123; // key is integer
var imdb = [{"123":"hi"}, {"234":"hello"}];
var valEventTime = "123";
var obj = imdb.some((val) => {
return Object.keys(val).includes(valEventTime);
})
console.log(obj);
You can use Array.some()
to determine if an array contains a value. For each item you want to use the Array.includes()
function to check for your variable for the items return from Object.keys()
on each entry. In your example your variable is an integer and the key is a string - you may want to be more specific in your matching.
var imdb = [{"123":"hi"}, {"234":"hello"}];
var valEventTime = "123"; // use a string to match the keys
// use Array.some() to loop through each item and short circuit when we return true
var hasKey = imdb.some(function(movie) {
// get the object keys as an array and see if that array contains your variable
// returning true will also return true from Array.some()
return Object.keys(movie).includes(valEventTime);
});