Получить результат элементов массива, которые не совпадают в одном массиве - PullRequest
0 голосов
/ 29 мая 2019

Я новичок в Javascript.У меня есть следующий массив результатов Mongodb

Result = [
          {
            "userId": "5cecfab4219aca350421b063",
            "providers": [
              "1689736266",
              "1598763690",
              "1528069614",
              "1831364272",
              "1548463045",
              "1245301159",
              "1386616399",
              "1790775971",
              "1629462130",
              "1992169783"
            ],
            "countByType": {
              "doctors": 6,
              "labs": 0,
              "hospitals": 0,
              "imagingCenters": 0,
              "other": 4
            },
            "id": "5cecfab4219aca350421b066"
          }
         ]

У меня есть другой массив

newArray = [1689736266, 1831364272, 123456789, 235695654 ];

как получить значение массива в newArray, которого нет в массиве провайдеров MongoDBResult.

Например: anotherArray = [123456789, 235695654];

Также, как я могу получить результат Array для провайдеров, используя Javascript.

Ответы [ 2 ]

1 голос
/ 29 мая 2019

Вы можете использовать filter() на newArray и проверить, присутствует ли он в массиве провайдеров Result, используя includes()

const Result = [ { "userId": "5cecfab4219aca350421b063", "providers": [ "1689736266", "1598763690", "1528069614", "1831364272", "1548463045", "1245301159", "1386616399", "1790775971", "1629462130", "1992169783" ], "countByType": { "doctors": 6, "labs": 0, "hospitals": 0, "imagingCenters": 0, "other": 4 }, "id": "5cecfab4219aca350421b066" } ]
         
const newArray = [1689736266, 1831364272, 123456789, 235695654 ];
const anotherArray = newArray.filter(x => !Result[0].providers.includes(String(x)))

console.log(anotherArray)
0 голосов
/ 29 мая 2019

Использование filter():

 

const providers =  [
          "1689736266",
          "1598763690",
          "1528069614",
          "1831364272",
          "1548463045",
          "1245301159",
          "1386616399",
          "1790775971",
          "1629462130",
          "1992169783"
        ];

const newArray = [1689736266, 1831364272, 123456789, 235695654 ];

const res = providers.filter(e => !newArray.includes("" + e));
console.log(res);
...