admin管理员组文章数量:1398840
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);
});
本文标签: javascriptNodeJS How to check if a key exists in an array of JSON objectStack Overflow
版权声明:本文标题:javascript - NodeJS: How to check if a key exists in an array of JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744644158a2617297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论