简体   繁体   English

如何仅获取要在 react.js 中显示的特定变化数据

[英]How to get only the specific variation data to be display in react.js

I have created Shopping Cart the product name, image, price, and attributes are created in separate components.我创建了购物车,产品名称、图像、价格和属性是在单独的组件中创建的。 I'm getting an issue where I wanted to get only the specific product variation's attribute, but I got all product variation attributes as below image我遇到了一个问题,我只想获得特定产品变体的属性,但我获得了所有产品变体属性,如下图所示

在此处输入图片说明

Above show the product and its details, but the product attributes must show only its specific variation attributes but it shows all its variation attributes.上面显示产品及其详细信息,但产品属性必须仅显示其特定变体属性,但显示其所有变体属性。 below is the code and the MongoDB database structure of the product.下面是产品的代码和MongoDB数据库结构。

在此处输入图片说明

在此处输入图片说明

I don't know where I go wrong, plz help me.我不知道我哪里错了,请帮助我。 thank you in advance.先感谢您。

You can use the array filter method if you want to get a subset of the attributes.如果要获取属性的子集,可以使用数组过滤器方法。 For example:例如:

const res = {
  data: {
    VariationList: {
      Attribute: {
        Weight: 1,
        Length: 2,
        Width: 3,
        Hook: 4
      }
    }
  }
};

function getProductAttributes(data, filterList = []) {
  const entries = Object.entries(data.VariationList.Attribute);
  if (!filterList.length) return entries;
  return entries.filter((attr) => filterList.includes(attr[0]));
}

let output = getProductAttributes(res.data);
console.log(output); // [ [ 'Weight', 1 ], [ 'Length', 2 ], [ 'Width', 3 ], [ 'Hook', 4 ] ]

output = getProductAttributes(res.data, ['Weight', 'Length']);
console.log(output); // [ [ 'Weight', 1 ], [ 'Length', 2 ] ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM