简体   繁体   English

比较两个数组,取出重复项

[英]Compare two arrays, get out the duplicates

I built a courses page with 3 courses, i want to build it in a way that all the courses are displayed with a buy button, the course info is collected from the database via the products collection. 我建立了一个包含3门课程的课程页面,我想要以一种显示所有课程并带有购买按钮的方式来构建它,该课程信息是通过products集合从数据库中收集的。 then i want that if the user bought the course, instead of buy, watch will be displayed. 然后我希望如果用户购买了课程,而不是购买,则将显示手表。

to do that i have 2 arrays: 1) all the products 2) the products that the user bought 为此,我有2个数组:1)所有产品2)用户购买的产品

now i want to compare them, and delete from the first array all the products that the user already bought. 现在,我想比较它们,并从第一个数组中删除用户已经购买的所有产品。

i tried checking online for methods, but i didn't understand them at all. 我尝试在线检查方法,但我根本不了解它们。

Here is the function to get the arrays: 这是获取数组的函数:

const productRef = await db.collection("products").get();

  const products = await productRef.docs.map(doc => {
    return {
      id: doc.id,
      name: doc.data().name,
      price: doc.data().price
    };
  });
  console.log(products);
//[ { id: 'course1', name: 'Course 1', price: 25 },
  { id: 'course2', name: 'Course 2', price: 10 },
  { id: 'course3', name: 'Course 3', price: 30 } ]


  if (user) {
    const claims = res.locals.decodedClaims;
    const uid = claims.uid;
    const email = claims.email;

    const userProductsRef = await db
      .collection("users")
      .doc(uid)
      .collection("courses")
      .get();

        if (userProductsRef.docs.length > 0) {
      const userProducts = userProductsRef.docs.map(doc => {
        return { id: doc.id, name: doc.data().name };
      });
      console.log(userProducts);//[ { id: 'course1', name: 'Course 1' } ]

     ///COMPARE HERE the two arrays
    }
  }

now i want to compare products with userProducts by the id. 现在,我想通过id将产品与userProducts进行比较。

so expected result at the end should be something like: 所以最终的预期结果应该是这样的:

products= [ 
  { id: 'course2', name: 'Course 2', price: 10 },
  { id: 'course3', name: 'Course 3', price: 30 } ];

userProducts=  [ { id: 'course1', name: 'Course 1' } ]

Thank You! 谢谢!

You can filter the products array by checking to see if each product id is in the userProducts array: 您可以通过检查每个产品ID是否在userProducts数组中来过滤products数组:

const filteredProducts = products
  .filter(prod => !userProducts.find(userProd => userProd.id === prod.id))

console.log(filteredProducts)

// [ 
//   { id: 'course2', name: 'Course 2', price: 10 },
//   { id: 'course3', name: 'Course 3', price: 30 }
// ]

I hope this is what you were looking for. 我希望这是您想要的。

You could use a combination of .filter() and .find() to achieve what you want: 您可以结合使用.filter().find()来实现所需的功能:

 const products = [ { id: 'course1', name: 'Course 1', price: 25 }, { id: 'course2', name: 'Course 2', price: 10 }, { id: 'course3', name: 'Course 3', price: 30 }, ]; const userProducts = [ { id: 'course1', name: 'Course 1' } ]; const result = products.filter(product => { return !userProducts.find(o => o.id === product.id); }); console.log(result); 

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

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