0

So my array looks like this:

let array = [ {"object1":1}, {"object2":2}, {"object3":3} ]; 

What I want to do is to check, for example, whether or not "object1" exists. The way I would prefer is pure Javascript.

I am doing this for large chunks of data and so my code needs to be something like this:

if ("opensprint1" in array){ console.log("yes, this is in the array"); } else { console.log("no, this is not in the array"); }; 

NOTE: I have tried to use the (in) function in JS and the (hasOwnProperty) and neither has worked.

Any ideas?

    5 Answers 5

    1
    if ("opensprint1" in array){ 

    That check for the array keys, so it would work with:

    if ("0" in array){ 

    But actually you want to check if some of the array elements got that key:

    if(array.some( el => "opensprint1" in el)) 
    1
    • .some is by far the best way, I think the implementation behind is like I said in my answer
      – Emeeus
      CommentedJul 24, 2018 at 20:06
    0

    You're trying to filter an array of objects. You can pass a custom function into Array.prototype.filter, defining a custom search function. It looks like you want to search based on the existence of keys. If anything is returned, that key exists in the object array.

    let array = [{ "object1": 1 }, { "object2": 2 }, { "object3": 3 } ]; const filterByKey = (arr, keyName) => array.filter(obj => Object.keys(obj).includes(keyName)).length > 0; console.log(filterByKey(array, 'object1')); console.log(filterByKey(array, 'object5'));

    That is roughly equivalent to:

    let array = [{ "object1": 1 }, { "object2": 2 }, { "object3": 3 } ]; const filterByKey = (arr, keyName) => { // iterate each item in the array for (let i = 0; i < arr.length; i++) { const objectKeys = Object.keys(arr[i]); // take the keys of the object for (let j = 0; j < objectKeys.length; j++) { // see if any key matches our expected if(objectKeys[i] === keyName) return true } } // none did return false; } console.log(filterByKey(array, 'object1')); console.log(filterByKey(array, 'object5'));

    1
    • this works - thank you so much for all of your help. thank you @hodrobond
      – Nicole M
      CommentedJul 24, 2018 at 22:10
    0

    This might help you

    let array = [ {"object1":1}, {"object2":2}, {"object3":3} ]; let targetkey = "opensprint1"; let exists = -1; for(let i = 0; i < array.length; i++) { let objKeys = Object.keys(array[i]); exists = objKeys.indexOf(targetkey); if (exists >= 0) { break; } } if (exists >= 0) { console.log("yes, this is in the array"); } else { console.log("no, this is not in the array"); } 
    0
      0
      let array = [ { "object1": 1 }, { "object2": 2 }, { "object3": 3 } ]; let checkKey = (key) => { var found = false; array.forEach((obj) => { if (!(obj[key] === undefined)) { found = true; array.length = 0; } }); return found; } console.log(checkKey("object2")); 
        0

        In this case, I think one of the most efficient way is to do a for and break like:

        let array = [ {"object1":1}, {"object2":2}, {"object3":3} ]; exist = false; for(let i = 0; i<array.length; i++){ if("object1" in array[i]){ exist = true;//<-- We just know the answer we want break;//<-- then stop the loop } } console.log(exist);

        When iteration finds a true case, stops the iteration. We can't perform a break in .map, .filter etc. So the number of iterations are the less possible. I think this is also the case of .some()

        1
        • if(array[i].object1){ is a bit dangerous here as { object1: 0 } wont be matchedCommentedJul 24, 2018 at 20:12

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.