Javascript : How to check if particular key is exist or not












2














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.










share|improve this question





























    2














    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.










    share|improve this question



























      2












      2








      2







      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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
























          6 Answers
          6






          active

          oldest

          votes


















          3














          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));








          share|improve this answer





























            2














            let id = 164854;
            result = myArray.filter((val) => {return val.id === id && val.data;});





            share|improve this answer





























              1














              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"))








              share|improve this answer





























                1














                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))








                share|improve this answer





























                  1














                  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));








                  share|improve this answer





























                    1














                    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)





                    share|improve this answer





















                      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
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      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









                      3














                      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));








                      share|improve this answer


























                        3














                        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));








                        share|improve this answer
























                          3












                          3








                          3






                          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));








                          share|improve this answer












                          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));






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 6:46









                          WeedozeWeedoze

                          9,3712337




                          9,3712337

























                              2














                              let id = 164854;
                              result = myArray.filter((val) => {return val.id === id && val.data;});





                              share|improve this answer


























                                2














                                let id = 164854;
                                result = myArray.filter((val) => {return val.id === id && val.data;});





                                share|improve this answer
























                                  2












                                  2








                                  2






                                  let id = 164854;
                                  result = myArray.filter((val) => {return val.id === id && val.data;});





                                  share|improve this answer












                                  let id = 164854;
                                  result = myArray.filter((val) => {return val.id === id && val.data;});






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 13 '18 at 6:51









                                  Anjana GAnjana G

                                  843




                                  843























                                      1














                                      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"))








                                      share|improve this answer


























                                        1














                                        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"))








                                        share|improve this answer
























                                          1












                                          1








                                          1






                                          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"))








                                          share|improve this answer












                                          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"))






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 13 '18 at 6:49









                                          AnoopAnoop

                                          19.1k94768




                                          19.1k94768























                                              1














                                              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))








                                              share|improve this answer


























                                                1














                                                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))








                                                share|improve this answer
























                                                  1












                                                  1








                                                  1






                                                  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))








                                                  share|improve this answer












                                                  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))






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 13 '18 at 6:49









                                                  Nitish NarangNitish Narang

                                                  2,948815




                                                  2,948815























                                                      1














                                                      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));








                                                      share|improve this answer


























                                                        1














                                                        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));








                                                        share|improve this answer
























                                                          1












                                                          1








                                                          1






                                                          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));








                                                          share|improve this answer












                                                          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));






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 13 '18 at 6:52









                                                          amrender singhamrender singh

                                                          4,8131718




                                                          4,8131718























                                                              1














                                                              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)





                                                              share|improve this answer


























                                                                1














                                                                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)





                                                                share|improve this answer
























                                                                  1












                                                                  1








                                                                  1






                                                                  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)





                                                                  share|improve this answer












                                                                  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)






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Nov 13 '18 at 7:29









                                                                  HarisHaris

                                                                  679




                                                                  679






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      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.




                                                                      draft saved


                                                                      draft discarded














                                                                      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





















































                                                                      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







                                                                      這個網誌中的熱門文章

                                                                      Xamarin.form Move up view when keyboard appear

                                                                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                                                      Anylogic : not able to use stopDelay()