在JavaScript中循环遍历数组,查找常见事件

时间:2017-12-03 20:10:13

标签: javascript arrays

每隔2.5秒我从服务器获取一个数组,该数组包含用户对象。我将每个数组分配给全局变量,10秒后将有4个数组包含以下数据。我想检查一个用户是否在所有这些数组中,如果任何用户出现在多个数组中,我将创建一个单独的数组并循环,然后在屏幕上显示,否则丢弃数组。我收集了数据。它就像RTLS一样,确保用户在10秒的时间内出现。我无法弄清楚如何在JavaScript中做到这一点。非常感谢您的见解。谢谢

[
    [
        {
            "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",
            "personName": "JOHN"           
        },
        {
           "id": "1b1f7caf-8bed-4f1d-8c71-a83a0131e71c",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",        
            "personName": "JANE"

        },
        {
            "id": "a6aec29a-a7fa-4d29-82cb-a83b00fedd36",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",       
            "personName": "Sara",          
        }
    ],
    [
        {
            "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",
            "personName": "JOHN"           
        },

        {
            "id": "a6aec29a-a7fa-4d29-82cb-a83b00fedd36",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",       
            "personName": "JANE"           
        }
    ],
    [
        {
            "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",
            "personName": "MIKE"           
        }   ,
        {
            "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",
            "personName": "JOHN"           
        },
        {
            "id": "a6aec29a-a7fa-4d29-82cb-a83b00fedd36",
            "proximity": "NEAR",
            "dateTime": "2017-12-03T14:40:02.5157777",       
            "personName": "JANE"           
        }       
    ]
]

2 个答案:

答案 0 :(得分:2)

您描述的问题有一个特殊的属性:如果所有数组中都包含任何用户,则第一个数组中会出现

  

要完成此任务,如果需要单个用户,可以使用Array#find(),如果需要,可以使用Array#filter()。

第二个属性:如果用户在任何数组中丢失,您将丢弃数组。

  

要完成此操作,请使用Array#some(),测试外部用户(来自上方)是否不属于任何其他数组。如果some()返回true,则过滤掉用户。

答案 1 :(得分:0)

这是一个简单的算法,可检测用户是否在所有这些数组中,如果有任何用户出现在多个数组中



var data = [
  [{
      "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
      "proximity": "NEAR",
      "dateTime": "2017-12-03T14:40:02.5157777",
      "personName": "JOHN"
    },
    {
      "id": "1b1f7caf-8bed-4f1d-8c71-a83a0131e71c",
      "proximity": "NEAR",
      "dateTime": "2017-12-03T14:40:02.5157777",
      "personName": "JANE"

    },
    {
      "id": "a6aec29a-a7fa-4d29-82cb-a83b00fedd36",
      "proximity": "NEAR",
      "dateTime": "2017-12-03T14:40:02.5157777",
      "personName": "Sara",
    }
  ],
  [{
      "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
      "proximity": "NEAR",
      "dateTime": "2017-12-03T14:40:02.5157777",
      "personName": "JOHN"
    },

    {
      "id": "a6aec29a-a7fa-4d29-82cb-a83b00fedd36",
      "proximity": "NEAR",
      "dateTime": "2017-12-03T14:40:02.5157777",
      "personName": "JANE"
    }
  ],
  [{
    "id": "6aa35f46-bf31-481e-bac5-a83b012ed1f0",
    "proximity": "NEAR",
    "dateTime": "2017-12-03T14:40:02.5157777",
    "personName": "MIKE"
  }]
]

function findCommon(arr, len) {
  const flat = arr
    .map(dimension => dimension.map(e => e.id))
    .reduce((uniq, collection) => {
      collection.forEach(element => {
        if (uniq[element]) uniq[element]++
        else uniq[element] = 1
      });
      return uniq
    }, {})

  return Object.keys(flat).filter(k => flat[k] === len)
}
console.log('present in all', findCommon(data, data.length));
console.log('present in 2', findCommon(data, 2));