java 用另一个数组过滤一个数组[已关闭]

mum43rcc  于 2022-12-25  发布在  Java
关注(0)|答案(2)|浏览(214)

2天前关闭。
Improve this question
我有两个数组arr1是:
数组1 =[{img:"/shoe1.webp ",颜色:"蓝色",价格:950,按钮:“添加到购物车”,名称:"男式运动鞋蓝",尺码:"10 "},{img:"/shoe2.webp ",颜色:"红色",价格:850,按钮:“添加到购物车”,名称:"男式运动鞋红色",尺码:"10 "},{img:"/shoe3.webp ",颜色:"黄色",价格:700,按钮:“添加到购物车”,名称:"男式运动鞋黄色",尺码:"10 "},{img:"/shoe1.webp ",颜色:"蓝色",价格:600,按钮:“添加到购物车”,名称:"男式运动鞋蓝",尺码:"9 "},{img:"/shoe2.webp ",颜色:"红色",价格:550,按钮:“添加到购物车”,名称:"男式运动鞋红色",尺码:"9 "},{img:"/shoe3.webp ",颜色:"黄色",价格:500,按钮:“添加到购物车”,名称:"男式运动鞋黄色",尺码:"9 "},{img:"/shoe1.webp ",颜色:"蓝色",价格:400,按钮:“添加到购物车”,名称:"男式运动鞋蓝",尺码:"8 "},{img:"/shoe2.webp ",颜色:"红色",价格:350,btn:"添加到购物车",名称:"男式运动鞋红色",尺码:"8 "},{img:"/shoe3.webp ",颜色:"黄色",价格:300,按钮:“添加到购物车”,名称:"男式运动鞋黄色",尺码:"8 "},{img:"/shoe1.webp ",颜色:"蓝色",价格:280,按钮:“添加到购物车”,名称:"男式运动鞋蓝",尺码:"7 "},{img:"/shoe2.webp ",颜色:"红色",价格:250,按钮:“添加到购物车”,名称:"男式运动鞋红色",尺码:"7 "},{img:"/shoe3.webp ",颜色:"黄色",价格:200,按钮:“添加到购物车”,名称:"男式运动鞋黄色",尺码:"7 "},{img:"/shoe1.webp ",颜色:"蓝色",价格:170,按钮:“添加到购物车”,名称:"男式运动鞋蓝",尺码:"6 "},{img:"/shoe2.webp ",颜色:"红色",价格:150,按钮:“添加到购物车”,名称:"男式运动鞋红色",尺码:"6 "},{img:"/shoe3.webp ",颜色:"黄色",价格:120,btn:"添加到购物车",名称:"男式运动鞋黄色",尺码:"6 "}
]第二个数组为:
数组2 =["6","7"]
我想用数组2过滤数组1
我想要这个结果
结果=[

{
    img: "/shoe1.webp",
    color: "blue",
    price: 170,
    btn: "Add to Cart",
    name: "Men Sneaker Blue",
    size: "6"
  },
  {
    img: "/shoe2.webp",
    color: "red",
    price: 150,
    btn: "Add to Cart",
    name: "Men Sneaker Red",
    size: "6"
  },
  {
    img: "/shoe3.webp",
    color: "yellow",
    price:120,
    btn: "Add to Cart",
    name: "Men Sneaker Yellow",
    size: "6"
  },
 {
    img: "/shoe1.webp",
    color: "blue",
    price: 280,
    btn: "Add to Cart",
    name: "Men Sneaker Blue",
    size: "7"
  },
  {
    img: "/shoe2.webp",
    color: "red",
    price: 250,
    btn: "Add to Cart",
    name: "Men Sneaker Red",
    size: "7"
  },
  {
    img: "/shoe3.webp",
    color: "yellow",
    price: 200,
    btn: "Add to Cart",
    name: "Men Sneaker Yellow",
    size: "7"
  }
]



 how do i get this result in react functional component
ej83mcc0

ej83mcc01#

您可以将Array#filterArray#includes结合使用。

let Array1 = [ { img: "/shoe1.webp", color: "blue", price: 950, btn: "Add to Cart", name: "Men Sneaker Blue", size: "10" }, { img: "/shoe2.webp", color: "red", price: 850, btn: "Add to Cart", name: "Men Sneaker Red", size: "10" }, { img: "/shoe3.webp", color: "yellow", price: 700, btn: "Add to Cart", name: "Men Sneaker Yellow", size: "10" }, { img: "/shoe1.webp", color: "blue", price: 600, btn: "Add to Cart", name: "Men Sneaker Blue", size: "9" }, { img: "/shoe2.webp", color: "red", price: 550, btn: "Add to Cart", name: "Men Sneaker Red", size: "9" }, { img: "/shoe3.webp", color: "yellow", price: 500, btn: "Add to Cart", name: "Men Sneaker Yellow", size: "9" }, { img: "/shoe1.webp", color: "blue", price: 400, btn: "Add to Cart", name: "Men Sneaker Blue", size: "8" }, { img: "/shoe2.webp", color: "red", price:350, btn: "Add to Cart", name: "Men Sneaker Red", size: "8" }, { img: "/shoe3.webp", color: "yellow", price: 300, btn: "Add to Cart", name: "Men Sneaker Yellow", size: "8" }, { img: "/shoe1.webp", color: "blue", price: 280, btn: "Add to Cart", name: "Men Sneaker Blue", size: "7" }, { img: "/shoe2.webp", color: "red", price: 250, btn: "Add to Cart", name: "Men Sneaker Red", size: "7" }, { img: "/shoe3.webp", color: "yellow", price: 200, btn: "Add to Cart", name: "Men Sneaker Yellow", size: "7" }, { img: "/shoe1.webp", color: "blue", price: 170, btn: "Add to Cart", name: "Men Sneaker Blue", size: "6" }, { img: "/shoe2.webp", color: "red", price: 150, btn: "Add to Cart", name: "Men Sneaker Red", size: "6" }, { img: "/shoe3.webp", color: "yellow", price:120, btn: "Add to Cart", name: "Men Sneaker Yellow", size: "6" }],
Array2 = ["6","7"];
let res = Array1.filter(({size}) => Array2.includes(size));
console.log(res);
4uqofj5v

4uqofj5v2#

Array1.filter((image) => Array2.includes(image.size))

相关问题