[英]How to match array with comma seperated values in Javascript?
I have two arrays, one for categories and other for products.我有两个数组,一个用于类别,另一个用于产品。 Products contain multiple categories as a comma-separated string.
产品以逗号分隔的字符串形式包含多个类别。 Now I want to match a particular category and add the matched product to an array to each category.
现在我想匹配一个特定的类别并将匹配的产品添加到每个类别的数组中。
I want to match the category to product_category.我想将类别与 product_category 相匹配。
Here are the arrays这是数组
Products产品
[
{
"id": 1,
"product_title": "Product 1",
"product_size": null,
"product_author": 5,
"description": "Test Description",
"product_category": "Shirts,Pents,Salwar",
"createdAt": "2020-02-08T11:42:15.000Z",
"updatedAt": "2020-02-08T11:42:15.000Z"
},
{
"id": 4,
"product_title": "Product 2",
"product_size": null,
"product_author": 5,
"description": "Test Description",
"product_category": "Pents,Salwar",
"createdAt": "2020-02-08T11:42:15.000Z",
"updatedAt": "2020-02-10T07:08:23.000Z"
}
]
Categories类别
[
{
"id": 1,
"category_name": "Shirts",
"createdAt": "2020-02-08T04:59:59.000Z",
"updatedAt": "2020-02-10T06:50:05.000Z"
},
{
"id": 9,
"category_name": "Pents",
"createdAt": "2020-02-08T04:59:59.000Z",
"updatedAt": "2020-02-10T06:50:05.000Z"
}
]
Code that I try but not working我尝试但不起作用的代码
this.categories.forEach(cat => {
this.products.filter(prod => {
if (prod.product_category.split(',').indexOf(cat.category_name) !== -1) {
this.categories.push(prod);
}
});
});
Please help me to solve this issue.请帮我解决这个问题。
Any solution appreciated!任何解决方案表示赞赏!
You could use .map()
on categories
to add a products
property to it.您可以在
categories
上使用.map()
为其添加products
属性。 This property will be the result of a .filter()
on the products
.此属性将是
products
上.filter()
的结果。
const categoriesWithProducts = categories.map(cat => {
return {
...cat,
products: products.filter(prod => {
return prod.product_category.split(',').includes(cat.category_name);
})
};
});
``
You do not need filter()
, you can use another forEach()
with includes()
and Object.assign()
:你不需要
filter()
您可以使用另一种forEach()
与includes()
和Object.assign()
var products = [ { "id": 1, "product_title": "Product 1", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Shirts,Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-08T11:42:15.000Z" }, { "id": 4, "product_title": "Product 2", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-10T07:08:23.000Z" } ]; var categories = [ { "id": 1, "category_name": "Shirts", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" }, { "id": 9, "category_name": "Pents", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" } ] categories.forEach((cat,i) => { products.forEach(prod => { if (prod.product_category.split(',').includes(cat.category_name)) { Object.assign(categories[i], prod); } }); }); console.log(categories);
One of the approaches is to create a new array where you would push products mapped to specific categories:其中一种方法是创建一个新数组,您可以在其中推送映射到特定类别的产品:
var products = [ { "id": 1, "product_title": "Product 1", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Shirts,Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-08T11:42:15.000Z" }, { "id": 4, "product_title": "Product 2", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-10T07:08:23.000Z" } ]; var categories = [ { "id": 1, "category_name": "Shirts", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" }, { "id": 9, "category_name": "Pents", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" } ] var productCategories = []; categories.forEach((cat)=> { var productCategory = {}; productCategory.category = cat; productCategory.products = []; products.forEach(prod => { if (prod.product_category.indexOf(cat.category_name) !== -1) { productCategory.products.push(prod); } }); productCategories.push(productCategory); console.log(productCategories); });
Try this尝试这个
categories.forEach(function(c){
c.products=products.filter(function(p){
var reg=new RegExp(c.category_name,'gi');
return reg.test(p.product_category)
})
})
Without Regex没有正则表达式
categories.forEach(function(c){
c.products=products.filter(function(p){
return (p.product_category.split(',').indexOf(c.category_name)!==-1)
})
})
You will get the products
inside each category
您将获得每个
category
的products
You can easily loop through all categories and then loop throgh all products.您可以轻松地遍历所有类别,然后遍历所有产品。 For each product check if category name is present and if so add product to category products:
对于每个产品,检查类别名称是否存在,如果存在,则将产品添加到类别产品:
this.categories.forEach(cat => {
cat.products = [];
this.products.forEach(prod => {
if (prod.product_category.split(',').indexOf(cat.category_name) !== -1) {
cat.products.push(prod);
}
});
});
Please note that I changed filter to forEach.请注意,我将过滤器更改为 forEach。
You can use Array#filter
to filter products on catagory names using Array#some
您可以使用
Array#filter
使用Array#some
过滤类别名称上的产品
let products = [{"id":1,"product_title":"Product 1","product_size":null,"product_author":5,"description":"Test Description","product_category":"Shirts,Pents,Salwar","createdAt":"2020-02-08T11:42:15.000Z","updatedAt":"2020-02-08T11:42:15.000Z"},{"id":4,"product_title":"Product 2","product_size":null,"product_author":5,"description":"Test Description","product_category":"Pents,Salwar","createdAt":"2020-02-08T11:42:15.000Z","updatedAt":"2020-02-10T07:08:23.000Z"}] let catagories = [{"id":1,"category_name":"Shirts","createdAt":"2020-02-08T04:59:59.000Z","updatedAt":"2020-02-10T06:50:05.000Z"},{"id":9,"category_name":"Pents","createdAt":"2020-02-08T04:59:59.000Z","updatedAt":"2020-02-10T06:50:05.000Z"}] let catagoryNames = new Set(catagories.map(e => e.category_name)); let filtered = [...products].filter(e => e.product_category.split(',').some(cat => catagoryNames.has(cat))); console.log(filtered)
Try this code.I hope it will helps you.试试这个代码。我希望它会帮助你。
product = [ { "id": 1, "product_title": "Product 1", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Shirts,Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-08T11:42:15.000Z" }, { "id": 4, "product_title": "Product 2", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-10T07:08:23.000Z" } ] categories=[ { "id": 1, "category_name": "Shirts", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z", }, { "id": 9, "category_name": "Pents", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z", } ] this.categories.forEach(cat => { cat['product'] = [] this.product.filter(prod => { if (prod.product_category.split(',').indexOf(cat.category_name) !== -1) { cat['product'].push(prod); } }); }); console.log(this.categories)
const products = [ { "id": 1, "product_title": "Product 1", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Shirts,Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-08T11:42:15.000Z" }, { "id": 4, "product_title": "Product 2", "product_size": null, "product_author": 5, "description": "Test Description", "product_category": "Pents,Salwar", "createdAt": "2020-02-08T11:42:15.000Z", "updatedAt": "2020-02-10T07:08:23.000Z" } ]; const categories = [ { "id": 1, "category_name": "Shirts", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" }, { "id": 9, "category_name": "Pents", "createdAt": "2020-02-08T04:59:59.000Z", "updatedAt": "2020-02-10T06:50:05.000Z" } ]; const matchedProducts = []; products.map(prod=>{ const isCateoryMatched = categories.some(c=>{ return prod.product_category.includes(c.category_name) }); if(isCateoryMatched ){ matchedProducts.push(prod); } }) //if you want to push the matched products into categories categories.push(...matchedProducts) console.log('matchedProducts',matchedProducts); console.log('categories',categories);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.