获取不匹配到单个数组中的数组元素的结果

时间:2019-05-29 07:03:24

标签: javascript arrays loops iteration javascript-objects

我是Java的新手。我有以下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提供程序Array中则不在此数组中。

例如:anotherArray = [123456789,235695654];

另外,如何使用Javascript获取提供者数组结果。

2 个答案:

答案 0 :(得分:1)

您可以在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)

答案 1 :(得分:0)

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