【问题标题】:Cannot filter array into array无法将数组过滤成数组
【发布时间】:2020-01-21 03:12:22
【问题描述】:

我有类别数组,其中包含专业化的对象数组。当我试图按专业过滤它时,出现了问题(不能更改并保存所有数据的类别列表,它也发生了变化。当我从数据搜索列表中删除字母时不会回来)。我尝试了很多方法,最后代码变成了这样(我知道这不是很好的代码,但我尝试了很多东西,只是打字很累);

这是我的过滤方法:

this.setState(prevState => {
  const { categories } = prevState;

  let filtered = [];

  for (let i = 0; i < categories.length; i++) {
    let newCategory = categories[i];
    const { specializations } = categories[i];
    let newSpecs = [];
    for (let j = 0; j < specializations.length; j++) {
      const { specName } = specializations[j];
      if (specName.indexOf(search) !== -1) {
        newSpecs.push(specializations[j]);
      }
    };
    newCategory.specializations = newSpecs;
    if (newSpecs.length>0) {
      filtered.push(newCategory);
    }
  }

  return {resultCategories:filtered}
});

这就是我的渲染方式:

category.specializations.map(
    (spec, index) =>
      (index < 5 || collapsed[cat_index]) && (
        <Text
          onPress={() =>
            this.props.navigation.navigate("CreateOrderSecond", {
              spec: spec.id
            })
          }
          style={styles.specs}
          key={index}
        >
          {spec.specName}
        </Text>
      )
  );

类别看起来如何:

Category{
 avatar Image{...}
 categoryName   string
 created    string($date-time)
 id integer($int64)
 lastUpdated    string($date-time)
 specializations    [Specialization{
                       avatar   Image{...}
                       created  string($date-time)
                       id   integer($int64)
                       lastUpdated  string($date-time)
                       masterName   string
                       specName string
                     }]
 }

【问题讨论】:

  • 请说明您的实际 json 是什么样的以及您想要的 json 是什么?
  • 我包含了类别的 Swagger 模型。类别中有一系列专业化,我只需要通过搜索字符串过滤专业化列表
  • 您想要针对每个类别的专业化?
  • 是的,专业化必须保留在类别中
  • 看起来某些东西是通过引用而不是值传递的,所以当您只想使用值而不修改原始对象时,您的原始对象正在被修改。您必须传递/复制对象而不仅仅是分配它,这恰好是通过引用。 (我没有在这里详细介绍,但我认为这是最有可能发生的)

标签: javascript arrays reactjs react-native


【解决方案1】:

不确定这是否适合您,但也许可以利用您的 .filter.map 数组方法来精简代码:

const search = 'Your Search String';

const possible_slight_improvement = prevState => {
  const { categories } = prevState; // Pull categories out of prevState

  const resultCategories = categories
    .map(({ specializations = [] }) => specializations.filter(({ specName }) => specName === search))
    .filter(specializations => specializations.length > 0);

  // resultCategories will be an array of arrays that have specializations in them
  // categories will be unchanged
  return { resultCategories };
};

this.setState(possible_slight_improvement);

【讨论】:

  • 我试过这个。但是错误正在出现。我认为结构有点改变。我正在尝试重构您的代码
【解决方案2】:

你可以试试这个

category = {
  specializations : [
    {
      created : 2312121,
      id   : 1,
      lastUpdated  : "24-01-2020",
      masterName   : "ProductOne",
      specName :  "Food"
    },
    {
      created : 2312121,
      id   : 1,
      lastUpdated  : "24-01-2020",
      masterName   : "ProductTwo",
      specName :  "Sopping"
    },
    {
      created : 2312121,
      id   : 1,
      lastUpdated  : "24-01-2020",
      masterName   : "ProductThree",
      specName :  "Food"
    }
  ]
}
searchValue = "Food"
let results = category.specializations.filter(specialization => specialization.specName === searchValue)


console.log(results)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多