简体   繁体   English

如何将具有相同 Id 的多个值绑定到 angular 的单个数组中

[英]How to bind multiple values with same Id into single array in angular

I've this array我有这个数组

Products = [
0 : {category_Id : 12 , ProductId : 11, Product name : "Salwar"}
1 : {category_Id : 12 , ProductId : 12, Product name : "Saree"}
2 : {category_Id : 12 , ProductId : 13, Product name : "Dress"}
3 : {category_Id : 13 , ProductId : 14, Product name : "Sandals"}
4 : {category_Id : 13 , ProductId : 15, Product name : "Wedges"}]

I'm trying to bind products with same category Id into single array index for eg:我正在尝试将具有相同类别 ID 的产品绑定到单个数组索引中,例如:

 category[12] : [{ProductId : 11, Product name : "Salwar"},{ProductId : 12, Product name : "Saree"},{ProductId : 13, Product name : "Dress"}] 

 category[13] : [{ProductId : 14, Product name : "Sandals"},{ProductId : 15, Product name : "Wedges"}]

If I want to print products with category Id 13, Both Sandals and Wedges should be printed.如果我想打印类别 ID 为 13 的产品,则应打印凉鞋和坡跟鞋。 Any ideas on how to get that result?关于如何获得该结果的任何想法?

You can use Array.filter for that:您可以Array.filter使用Array.filter

 products = [ {category_Id : 12 , ProductId : 11, Product_name : "Salwar"}, {category_Id : 12 , ProductId : 12, Product_name : "Saree"}, {category_Id : 12 , ProductId : 13, Product_name : "Dress"}, {category_Id : 13 , ProductId : 14, Product_name : "Sandals"}, {category_Id : 13 , ProductId : 15, Product_name : "Wedges"} ]; getCategory(id){ return products.filter(p=>p.category_Id==id); } console.log(getCategory(12)); console.log('*************'); console.log(getCategory(13));

first filter equal identities, then map each item for desired outcome首先过滤相同的身份,然后映射每个项目以获得所需的结果

const category = (Products || [])
    .filter(product => product.ProductId === product.category_Id)
    .map(product => ({ ProductId: product.ProductId, "Product name": product["Product name"] }))

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

相关问题 在 Angular 8 中使用相同的 ngModel 属性在多个文本框中绑定不同的值 - Bind different values in multiple textbox with same ngModel property in Angular 8 如何将多个动作绑定到单个 Angular 输出? - How can I bind multiple actions to a single Angular output? 如何将具有不同值的相同属性的多个对象处理为angular2中的单个对象? - How to handle multiple objects having same property with different values into single object in angular2? 如何将编辑表单的值与 angular 5 中的 object 数组绑定? - How do I bind values of edit form with array of object in angular 5? 在 angular 6 中过滤具有多个值的相同键的数组对象 - Filtering array objects for same key with multiple values in angular 6 如何在Angular 2中删除数组中的多个值 - How to remove multiple values in an array in Angular 2 在角度6的倍数for循环内绑定值 - Bind values inside a multiple for loop in angular 6 从控件 Angular 6 将值绑定到 object 中的数组 - Bind values to array in an object from controls Angular 6 如何绑定到 angular 11 中的数组 - How to bind to array in angular 11 如何绑定Angular Select中* ngFor项目的对象ID? 目前,select绑定与插值do的值相同。 - How can I bind object id from *ngFor item in Angular Select? At the moment, the select bind the same value as interpolation do.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM