简体   繁体   English

用另一个数组过滤一个数组

[英]Filtering an Array with another Array

I'm pretty new to react, and I'm working on a product where I am trying to get the products and their details from my products array with the products within my cart filtered out.我对反应还很陌生,我正在开发一种产品,我试图从我的products数组中获取产品及其详细信息,并过滤掉购物车中的产品。 I've tried doing this a couple of ways, but I'm struggling to get this to work.我已经尝试过几种方法来做到这一点,但我正在努力让它发挥作用。 I have:我有:

import React, { useEffect } from 'react';
import Product from '../components/Product';
import {Link, useParams } from 'react-router-dom';
import { listProducts } from '../actions/productActions';


 export default function CarouselForProductPage(props) {
    const dispatch = useDispatch();
    const productList = useSelector((state) => state.productList);
    const { loading, error, products } = productList;
    const cart = useSelector((state) => state.cart);
    const { cartItems } = cart;
    useEffect(() =>{
        dispatch(listProducts({}));
    }, [dispatch]);

    
    var cart_ids = cartItems.map(product => ({ _id: product._id }));
     console.log(cart_ids);

    let res = products.filter(x => !cart_ids.includes(x));
    console.log(res);

   var carts = products.filter((x) => x._id !== cart_ids);
   console.log(carts);

    
  return (
    <div>
        
    </div>
  );
}


My cartItems variable is an array of the data of the products in my cart, and cart_ids are the ids associated with those products.我的cartItems变量是我购物车中产品数据的数组,而cart_ids是与这些产品关联的 ID。 The variable products list all of the data associate with each of my products.可变products列出了与我的每个产品关联的所有数据。 I'm trying to filter out the products from products that have the ids contained within cart_ids .我正在尝试从cart_ids中包含 id 的products中过滤掉产品。 I have tried doing this two different ways:我尝试过两种不同的方式:

let res = products.filter(x => !cart_ids.includes(x));
    console.log(res);

And

   var carts = products.filter((x) => x._id !== cart_ids);
   console.log(carts);

Both of these arrays (res and carts) are giving me results equal to the products array.这两个 arrays(资源和购物车)给我的结果等于products数组。

I would really appreciate any help or guidance on what I am doing wrong.Thank you!对于我做错的任何帮助或指导,我将不胜感激。谢谢!

cart_ids = [
0:
_id: "622ae8d94bee11259a3b1756"
[[Prototype]]: Object
1:
_id: "624bc1a50f91078261ab650d"
[[Prototype]]: Object
]
products = 
[0:
image: "/uploads/2022-03-11T06:15:00.618Zproduct-1.jpg"
name: "Bliss"
price: 40
__v: 5
_id: "622ae8d94bee11259a3b1756"
[[Prototype]]: Object
1:
image: "/uploads/2022-03-12T23:31:14.922Zproduct-2.jpg"
name: "sample name 1647127866131"
price: 30
__v: 1
_id: "622d2d3a4bee11259a3b17ed"
[[Prototype]]: Object
2:
image: "/uploads/2022-04-05T04:12:46.469Zproduct-4.jpg"
name: "sample name 1649131941926"
price: 50
__v: 1
_id: "624bc1a50f91078261ab650d"
[[Prototype]]: Object
3:
image: "/uploads/2022-04-05T04:39:38.449Zproduct-1.jpg"
name: "sample name 1649133566290"
price: 60
__v: 1
_id: "624bc7fe0f91078261ab6529"
[[Prototype]]: Object
4:
image: "/uploads/2022-04-05T04:40:31.140Zproduct-5.jpg"
name: "sample name 1649133604220"
price: 40
__v: 1
_id: "624bc8240f91078261ab6531"]

Result I am trying to get我想要得到的结果

[[0:
image: "/uploads/2022-03-12T23:31:14.922Zproduct-2.jpg"
name: "sample name 1647127866131"
price: 30
__v: 1
_id: "622d2d3a4bee11259a3b17ed"
[[Prototype]]: Object
1:
image: "/uploads/2022-04-05T04:39:38.449Zproduct-1.jpg"
name: "sample name 1649133566290"
price: 60
__v: 1
_id: "624bc7fe0f91078261ab6529"
[[Prototype]]: Object
2:
image: "/uploads/2022-04-05T04:40:31.140Zproduct-5.jpg"
name: "sample name 1649133604220"
price: 40
__v: 1
_id: "624bc8240f91078261ab6531"]]

cart_ids is an array of objects you need to parse to a string array first to use for includes . cart_ids 是一个对象数组,您需要首先将其解析为字符串数组以用于includes

const cardIdOnlyArray = cartIds.map(x => x._id)常量 cardIdOnlyArray = cartIds.map(x => x._id)

 const products = [ { image: '/uploads/2022-03-11T06:15:00.618Zproduct-1.jpg', name: 'Bliss', price: 40, __v: 5, _id: '622ae8d94bee11259a3b1756', }, { image: '/uploads/2022-03-12T23:31:14.922Zproduct-2.jpg', name: 'sample name 1647127866131', price: 30, __v: 1, _id: '622d2d3a4bee11259a3b17ed', }, { image: '/uploads/2022-04-05T04:12:46.469Zproduct-4.jpg', name: 'sample name 1649131941926', price: 50, __v: 1, _id: '624bc1a50f91078261ab650d', }, { image: '/uploads/2022-04-05T04:39:38.449Zproduct-1.jpg', name: 'sample name 1649133566290', price: 60, __v: 1, _id: '624bc7fe0f91078261ab6529', }, { image: '/uploads/2022-04-05T04:40:31.140Zproduct-5.jpg', name: 'sample name 1649133604220', price: 40, __v: 1, _id: '624bc8240f91078261ab6531', }, ] const cartIds = [ { _id: '622ae8d94bee11259a3b1756', }, { _id: '624bc1a50f91078261ab650d', }, ] const cardIdOnlyArray = cartIds.map(x => x._id) const result = products.filter(product =>.cardIdOnlyArray.includes(product._id)) console.log(result)

My guess is that in your first example its likely you forgot to include x._id in .cart_ids.includes(x) .我的猜测是,在您的第一个示例中,您可能忘记在.cart_ids.includes(x)中包含x._id By looking at the format of the products array defined above, that object should have _id prop present.通过查看上面定义的products数组的格式,object 应该存在_id属性。

So in first case it should work like所以在第一种情况下它应该像

let res = products.filter(x => !cart_ids.includes(x._id));

And second case doesn't make much sense since you are comparing an array with property (which is called _id and its likely, a string or number, and it wont work even if its a reference type such as object or array since comparing those data types with strict operator will always return false)第二种情况没有多大意义,因为您正在比较一个数组和属性(称为_id ,它可能是一个字符串或数字,并且即使它是引用类型(例如 object 或数组)也不会工作,因为比较这些数据具有严格运算符的类型将始终返回 false)

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

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