简体   繁体   English

我正在尝试遍历一个数组并找到在属性中具有所述关键字的每个对象

[英]I am trying to loop through an array and find each object that has said keyword in a property

So I want to set this variable to all of the objects in an array in which its category has a certain keyword.所以我想将此变量设置为数组中的所有对象,其中其类别具有某个关键字。 This is what I have so far这是我到目前为止

  let tempMensProducts = tempClothingProducts
          .filter(obj => obj.category.includes('Mens'))

export const clothingProducts = [
  {
    id: 1,
    title: 'COMME DES GARCONS TEE',
    img: 'img/product-1.png',
    img2: 'img/product-1-1.png',
    img3: 'img/product-1-2.png',
    img4: 'img/product-1-3.png',
    luxury: 'All Luxury items are inspected to verify authenticity',
    price: 200,
    info: ' COMME DES GARCONS PLAY BASIC LOGO TEE',
    inCart: false,
    count: 0,
    total: 0,
    fabric: '100% Cotton',
    category: 'Mens Fashion'
  }
]

You need to pass a callback to map to use it - but here, map isn't required at all.您需要传递一个回调到map才能使用它 - 但在这里,根本不需要map Just use filter .只需使用filter

let tempMensProducts = tempClothingProducts.filter(({ category }) => category.includes("Mens"));

If you try to use map without a callback, it'll result in an error as it tries to call its argument, and if you don't pass one, it'll try to do undefined() .如果您尝试使用没有回调的map ,它会在尝试调用其参数时导致错误,如果您不传递参数,它将尝试执行undefined()

 [1, 2, 3].map();

You can split the array by whitespaces and then apply array contains there to sure that it will give you correct result您可以通过空格拆分数组,然后在那里应用数组contains以确保它会给您正确的结果

I mean to say it should not return "Women" when keyword is "men"我的意思是说当关键字是“男人”时它不应该返回“女人”

 let clothingProducts = [ { id: 1, title: 'COMME DES GARCONS TEE', img: 'img/product-1.png', img2: 'img/product-1-1.png', img3: 'img/product-1-2.png', img4: 'img/product-1-3.png', luxury: 'All Luxury items are inspected to verify authenticity', price: 200, info: ' COMME DES GARCONS PLAY BASIC LOGO TEE', inCart: false, count: 0, total: 0, fabric: '100% Cotton', category: 'Mens Fashion' }, { id: 2, title: 'COMME DES GARCONS TEE', img: 'img/product-1.png', img2: 'img/product-1-1.png', img3: 'img/product-1-2.png', img4: 'img/product-1-3.png', luxury: 'All Luxury items are inspected to verify authenticity', price: 200, info: ' COMME DES GARCONS PLAY BASIC LOGO TEE', inCart: false, count: 0, total: 0, fabric: '100% Cotton', category: 'woMens Fashion' } ] let filter = clothingProducts.filter(c => c.category.split(" ").includes("Mens")); console.log(filter);

暂无
暂无

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

相关问题 我正在尝试将 object 添加到 object 中的数组末尾 - I am trying to add an object to the end of an array inside said object 数组有 5 个对象,每个对象都有另外 5 个对象的数组属性。 我正在尝试使用 ID 查找对象的函数 - Array has 5 objects and each object has an array property of another 5 objects. I'm trying to make function that finds the object with ID 我试图遍历对象数组并将其填充到表中 - I am trying to loop through an array of objects and populate them to a table 循环遍历对象数组以查找具有匹配属性的对象 - Loop through array of objects to find object with matching property 如何在具有对象的React App中循环遍历javascript数组并获得具有特定值的属性的计数 - How do I loop through a javascript Array in a React App that has object and get a count on a property that has a certain value 我正在尝试更新 object 数组的属性,但它显示无法分配给 object 的只读属性“isSelected” - I am trying to update a property of an array of object but it is showing Cannot assign to read only property 'isSelected' of object 循环遍历 arrays 数组中的每个 object 找到 id,将其与数组中的另一个 object 匹配并将它们连接在一起 - Loop through each object in a array of arrays find the id, match it to another object in an array and concatenate them together 我试图在向该 object 添加新属性之前将 object 推送到数组,但它直接更新数组 - i am trying to push an object to an array before adding a new property to that object , but it directly updates the array 找到一个类似的属性然后循环对象 - Find a similar property then loop through object 试图遍历对象数组并找到匹配项 - trying to loop through the array of objects and find the match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM