如果JSON和Array中存在相同的值,请执行某些操作

时间:2018-06-22 15:41:05

标签: javascript arrays json

我对逻辑有一个大概的了解,但是如果有人可以提供帮助,那就万分感谢!

尝试查看JSON(来自URL)和数组中的任何数字是否都匹配。如果可以,请执行某些操作。在此示例中,JSON和数组中都存在300。

可能的逻辑/代码

if user and bcrypt.check_password_hash(hashPwd, form.password.data):

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

// 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
}

数组

[
   {

   "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"
      }
      ]
   }

  }
]

5 个答案:

答案 0 :(得分:2)

只需使用嵌套循环

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

答案 1 :(得分:1)

您可以使用以下代码:

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

.forEach()对数组的每个元素执行指定的操作。您可以使用Object.keys()获取对象的所有键,然后使用.filter().includes()来获取仅与myArray匹配的键。然后,您可以再次使用.forEach()执行操作(在本例中为console.log)。由于+是字符串,因此在key之前使用number将其转换为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)));

cd android
gradlew clean

答案 2 :(得分:1)

您可以获取对象键,然后进行查找。

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

答案 3 :(得分:1)

您的JSON数据数组中有一个元素是一个对象。您想查看该对象的键之一是否在您的数组中,对吧?

您可以使用Object.keys()返回键数组,并使用Array.prototype.some()Array.prototype.includes()进行返回布尔值的测试:

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

答案 4 :(得分:0)

您将必须对数组进行排序并执行while循环。

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

接下来开始一会儿循环比较这两个项目。

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