0

I have a rough idea of the logic but if anyone can help, greatly appreciate it!

Trying to see if any of the numbers match in both the JSON (from a URL) and array. If it does, do something. In this example, 300 is the number that exists in both the JSON and array.

Possible logic / code

// FOR json
for(var i = 0; i < obj.length; i++) {
    var myObjNum = obj[i];
}

// FOR myArray
for(var i = 0; i < myArray.length; i++) {
    var myArrayNum = myArray[i];
}

// if cat ID is in the DOM and array, do something
if (myObjNum == myArrayNum) {
   // if a number is found in both the array and JSON, do something here
}

JSON ( https://api.myjson.com/bins/svr3i )

[
   {

   "5774": {
      "subCats": [
      {
         "town": "atlanta",
         "state": "georgia"
      }
      ]
   },
   "300": {
      "subCats": [
      {
         "town": "new york city",
         "state": "new york"
      }
      ]
   },
   "899": {
      "subCats": [
      {
         "town": "san diego",
         "state": "california"
      }
      ]
   },
   "2557": {
      "subCats": [
      {
         "town": "chicago",
         "state": "illinois"
      }
      ]
   }

  }
]

Array

var myArray = [500,4743,300,77899];

5 Answers 5

2

Just use nested loops

var JSON = [
   {

   "5774": {
      "subCats": [
      {
         "town": "atlanta",
         "state": "georgia"
      }
      ]
   },
   "300": {
      "subCats": [
      {
         "town": "new york city",
         "state": "new york"
      }
      ]
   },
   "899": {
      "subCats": [
      {
         "town": "san diego",
         "state": "california"
      }
      ]
   },
   "2557": {
      "subCats": [
      {
         "town": "chicago",
         "state": "illinois"
      }
      ]
   }

  }
]


var myArray = [500,4743,300,77899];

Object.keys(JSON[0]).forEach(key => {
      myArray.forEach(item => {
           if(item == key){
             // do something here
             console.log('action');
           }
      });
    });

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Hikmat! This one made the most sense to me, even though the other answers could be correct as well.
1

You can get the object keys and then do your lookups.

var data = [{
  "5774": {
    "subCats": [{
      "town": "atlanta",
      "state": "georgia"
    }]
  },
  "300": {
    "subCats": [{
      "town": "new york city",
      "state": "new york"
    }]
  },
  "899": {
    "subCats": [{
      "town": "san diego",
      "state": "california"
    }]
  },
  "2557": {
    "subCats": [{
      "town": "chicago",
      "state": "illinois"
    }]
  }
}];


var theKeysForTheArrayElements = Object.keys(data[0]);
var keysToLookFor = [500, 4743, 300, 77899];

console.log(keysToLookFor.filter(function(key){
  return theKeysForTheArrayElements.indexOf(key.toString()) > -1;
}));

Comments

1

You can use below code:

array.forEach(
    obj => Object.keys(obj)
                 .filter(key => myArray.includes(+key))
                 .forEach(key => console.log(key))); // do something

.forEach() executes specified action for each element of your array. You can use Object.keys() to get all keys for your objects and then .filter() with .includes() to get only keys matching with myArray. Then you can use .forEach() again to execute your action (which is console.log in this case). Using + befor key to convert it to number since it's string

var array = [
   {

   "5774": {
      "subCats": [
      {
         "town": "atlanta",
         "state": "georgia"
      }
      ]
   },
   "300": {
      "subCats": [
      {
         "town": "new york city",
         "state": "new york"
      }
      ]
   },
   "899": {
      "subCats": [
      {
         "town": "san diego",
         "state": "california"
      }
      ]
   },
   "2557": {
      "subCats": [
      {
         "town": "chicago",
         "state": "illinois"
      }
      ]
   }

  }
]

var myArray = [500, 4743, 300, 77899];

array.forEach(
    obj => Object.keys(obj)
                 .filter(key => myArray.includes(+key))
                 .forEach(key => console.log(key)));

Comments

1

Your array from the JSON data has one element which is an object. You want to see if one of that object's keys is in your array, right?

You can use Object.keys() which returns an array of keys and Array.prototype.some() and Array.prototype.includes() to do a test that will return a boolean:

const arr1 = [{"5774": {   "subCats": [   {      "town": "atlanta",      "state": "georgia"   }   ]},"300": {   "subCats": [   {      "town": "new york city",      "state": "new york"   }   ]},"899": {   "subCats": [   {      "town": "san diego",      "state": "california"   }   ]},"2557": {   "subCats": [   {      "town": "chicago",      "state": "illinois"   }   ]}}]

const arr2 = [500,4743,300,77899];

let test = Object.keys(arr1[0]).some(item => arr2.includes(parseInt(item)))

if(test) console.log("do something")

Comments

0

You will have to sort the array and do a while loop.

arr1.sort();
arr2.sort();

Next start a while loop comparing both items.

    const arr1 = [1, 2, 5, 9, 15, 21];
    const arr2 = [2, 4, 8, 10, 21, 24];
    
    let i=0;
    let j=0;
    const equalItems = [];
    while (i < arr1.length && j < arr2.length) {
      if (arr1[i] === arr2[j]) {
        equalItems.push(arr1[i]);
        i++;
        j++;
      } else if (arr1[i] > arr2[j]) {
        j++;
      } else {
        i++;
      }
    }
    
    console.log(equalItems);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.