[英]I am trying to add an object to the end of an array inside said object
[英]I am trying to loop through an array and find each object that has said keyword in a property
所以我想将此变量设置为数组中的所有对象,其中其类别具有某个关键字。 这是我到目前为止
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'
}
]
您需要传递一个回调到map
才能使用它 - 但在这里,根本不需要map
。 只需使用filter
。
let tempMensProducts = tempClothingProducts.filter(({ category }) => category.includes("Mens"));
如果您尝试使用没有回调的map
,它会在尝试调用其参数时导致错误,如果您不传递参数,它将尝试执行undefined()
。
[1, 2, 3].map();
您可以通过空格拆分数组,然后在那里应用数组contains
以确保它会给您正确的结果
我的意思是说当关键字是“男人”时它不应该返回“女人”
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.