简体   繁体   English

匹配数组与 mongoose 数组

[英]Match array with mongoose array

I am creating a project on node.js first time, I have been stuck in a problem that I am passing an array from the client-side(array A) and I have created an array on the backend too(Array b).我第一次在 node.js 上创建一个项目,我一直陷入一个问题,我从客户端传递一个数组(数组 A),我也在后端创建一个数组(数组 b)。 Now I want to check if "array A" has any other element than "array b" or all the elements of "array a" found in "array b".现在我想检查“array A”是否有除“array b”之外的任何其他元素或在“array b”中找到的“array a”的所有元素。 FYI, I am using NODE Express and mongoose. Thanks In Advance仅供参考,我正在使用 NODE Express 和 mongoose。提前致谢

If it is simply comparing 2 arrays, why not something like follows?如果只是简单地比较 2 arrays,为什么不像下面这样呢?

const arrayA = [1,2,3,4,5];
const arrayB = [1,2,3];

function arrayDifference(baseArray, comparedArray) {
  
  const difference = [];

  for(let element of   comparedArray) {
    if(baseArray.indexOf(element) !== -1) {
      continue;
    }
    difference.push(element);
  }
  return difference;
}

const diffA = arrayDifference(arrayA, arrayB);
console.log(diffA); // []

const diffB = arrayDifference(arrayB, arrayA);
console.log(diffB); // [4, 5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM