Javascript : How to check if particular key is exist or not
I am getting a response by calling API. which is like this.
[{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]},
{"id":564656,"data":[{"rate" : 40}]}]
How can I check that for particular id, there is a key data
exist or not?
Any help would be great.
Thank You.
javascript object
add a comment |
I am getting a response by calling API. which is like this.
[{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]},
{"id":564656,"data":[{"rate" : 40}]}]
How can I check that for particular id, there is a key data
exist or not?
Any help would be great.
Thank You.
javascript object
add a comment |
I am getting a response by calling API. which is like this.
[{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]},
{"id":564656,"data":[{"rate" : 40}]}]
How can I check that for particular id, there is a key data
exist or not?
Any help would be great.
Thank You.
javascript object
I am getting a response by calling API. which is like this.
[{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]},
{"id":564656,"data":[{"rate" : 40}]}]
How can I check that for particular id, there is a key data
exist or not?
Any help would be great.
Thank You.
javascript object
javascript object
edited Nov 13 '18 at 6:49
Hassan Imam
11.6k31230
11.6k31230
asked Nov 13 '18 at 6:44
Brijesh PatelBrijesh Patel
376
376
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
You can first use Array.find()
to get the object with the desired ID and then check if the property data
is undefined or not
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
add a comment |
let id = 164854;
result = myArray.filter((val) => {return val.id === id && val.data;});
add a comment |
you can use filter
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
add a comment |
You can use "Array.find" to check if id exists, and then you can convert the result to boolean by using "!!"
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
add a comment |
You can simply use Array.some(), it will return a boolean value based on the condition.
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
add a comment |
you can use findIndex function to find the particular id
let id = 241132;
let index = arr.findIndex((data)=> data.id == id)
index==-1 ? console.log("not find") : console.log("find index=>"+index)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275244%2fjavascript-how-to-check-if-particular-key-is-exist-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can first use Array.find()
to get the object with the desired ID and then check if the property data
is undefined or not
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
add a comment |
You can first use Array.find()
to get the object with the desired ID and then check if the property data
is undefined or not
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
add a comment |
You can first use Array.find()
to get the object with the desired ID and then check if the property data
is undefined or not
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
You can first use Array.find()
to get the object with the desired ID and then check if the property data
is undefined or not
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
answered Nov 13 '18 at 6:46
WeedozeWeedoze
9,3712337
9,3712337
add a comment |
add a comment |
let id = 164854;
result = myArray.filter((val) => {return val.id === id && val.data;});
add a comment |
let id = 164854;
result = myArray.filter((val) => {return val.id === id && val.data;});
add a comment |
let id = 164854;
result = myArray.filter((val) => {return val.id === id && val.data;});
let id = 164854;
result = myArray.filter((val) => {return val.id === id && val.data;});
answered Nov 13 '18 at 6:51
Anjana GAnjana G
843
843
add a comment |
add a comment |
you can use filter
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
add a comment |
you can use filter
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
add a comment |
you can use filter
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
you can use filter
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
let data = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]} ];
function hasData(key) { return !!data.filter(x=> x.id==key)[0].data }
console.log(hasData("213132"))
console.log(hasData("164854"))
answered Nov 13 '18 at 6:49
AnoopAnoop
19.1k94768
19.1k94768
add a comment |
add a comment |
You can use "Array.find" to check if id exists, and then you can convert the result to boolean by using "!!"
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
add a comment |
You can use "Array.find" to check if id exists, and then you can convert the result to boolean by using "!!"
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
add a comment |
You can use "Array.find" to check if id exists, and then you can convert the result to boolean by using "!!"
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
You can use "Array.find" to check if id exists, and then you can convert the result to boolean by using "!!"
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
var arr = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}]
function doesDataExist(id) {
return !!arr.find(d => d.id == id).data
}
console.log(doesDataExist(164854))
console.log(doesDataExist(213132))
answered Nov 13 '18 at 6:49
Nitish NarangNitish Narang
2,948815
2,948815
add a comment |
add a comment |
You can simply use Array.some(), it will return a boolean value based on the condition.
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
add a comment |
You can simply use Array.some(), it will return a boolean value based on the condition.
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
add a comment |
You can simply use Array.some(), it will return a boolean value based on the condition.
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
You can simply use Array.some(), it will return a boolean value based on the condition.
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
let arr= [{"id":213132},{"id":241132},{"id":465413},{"id":546351, "data":[{"id":1}]},{"id":164854}];
let id = 546351;
console.log(arr.some(obj => obj.id == id && !!obj.data));
id = 213132;
console.log(arr.some(obj => obj.id == id && !!obj.data));
answered Nov 13 '18 at 6:52
amrender singhamrender singh
4,8131718
4,8131718
add a comment |
add a comment |
you can use findIndex function to find the particular id
let id = 241132;
let index = arr.findIndex((data)=> data.id == id)
index==-1 ? console.log("not find") : console.log("find index=>"+index)
add a comment |
you can use findIndex function to find the particular id
let id = 241132;
let index = arr.findIndex((data)=> data.id == id)
index==-1 ? console.log("not find") : console.log("find index=>"+index)
add a comment |
you can use findIndex function to find the particular id
let id = 241132;
let index = arr.findIndex((data)=> data.id == id)
index==-1 ? console.log("not find") : console.log("find index=>"+index)
you can use findIndex function to find the particular id
let id = 241132;
let index = arr.findIndex((data)=> data.id == id)
index==-1 ? console.log("not find") : console.log("find index=>"+index)
answered Nov 13 '18 at 7:29
HarisHaris
679
679
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275244%2fjavascript-how-to-check-if-particular-key-is-exist-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown