如何在React状态数组中不包含重复值?

背风处

我想确保我不能将重复的值以React状态推入数组。尽管重复的值仍在数组中。

我曾尝试使用,.includes但无法正常工作。

const categoryCheck = (category) => {
  let categories = [...this.state.categories]
  if (this.state.categories === undefined) {
    return
  }
  console.log(categories, category)
  console.log(!categories.includes(category))
  if (!categories.includes(category) === false) {
    categories.push(category)
    console.log('new category', categories)
  } 
}

输入: 'cat'

预期结果:['cat']实际结果:['cat', 'cat']

更新:这是我的功能,这就是我的称呼方式。感谢您的所有帮助!

  uniqueCategories = category => {
    //makes sure that there are no duplicate categories in array
    if (category === undefined) {
      return;
    }
    let categories = new Set(category);
    categories = Array.from(categories);
    console.log(categories);
    return categories;
  };

我在这样的另一个函数中调用它:

  this.setState({
      categories: this.uniqueCategories([
        ...this.state.categories,
        categoryInput
      ])
戈戈伦
if (!categories.includes(category) === false) {

是双重否定的。删除=== false

一种替代方法是使用aSet作为唯一性。通常,集合提供快速的查找时间并自动拒绝重复项,但是对于少量数据,性能可能并不比使用的数组好includes

这是一个使用的玩具示例Set

const {useState} = React;

const Example = () => {
  const [categories, setCategories] = useState(new Set());
  const [item, setItem] = useState("");
  
  const addCategory = e => {
    if (item.trim()) {
      setCategories(new Set(categories).add(item.trim()));
    }
  };

  return (
    <div>
      <input
        onChange={e => setItem(e.target.value)}
        value={item}
      />&nbsp;
      <button onClick={addCategory}>Add Item</button>
      <ul>{[...categories].map(e => <li key={e}>{e}</li>)}</ul>
    </div>
  );
};

ReactDOM.render(<Example />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.development.js"></script>

还有一个includes例子:

const {useState} = React;

const Example = () => {
  const [categories, setCategories] = useState([]);
  const [item, setItem] = useState("");
  
  const addCategory = e => { 
    const trimmed = item.trim();
    
    if (trimmed && !categories.includes(trimmed)) {
      setCategories(categories.concat(trimmed));
    }
  };

  return (
    <div>
      <input
        onChange={e => setItem(e.target.value)}
        value={item}
      />&nbsp;
      <button onClick={addCategory}>Add Item</button>
      <ul>{categories.map(e => <li key={e}>{e}</li>)}</ul>
    </div>
  );
};

ReactDOM.render(<Example />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.development.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 React 状态数组中不包含重复值,如何防止数组中的值重复?

如何在C#中验证值的集合是否唯一(不包含重复项)

如何在numpy数组中重复值?

如何在 React 中的状态数组中仅输出数组的一个值?

在多维数组中查找重复值并创建不包含重复项的新多维数组

如何在React的类组件中为状态数组分配值

如何在React Native中为数组设置状态值?

如何过滤出数组中不包含特定值的单词?

如何在mongo中查找不包含数组元素的文档

在React中,如何在不映射整个对象的情况下获取数据并在状态数组中呈现单个对象?

如何在C中停止数组中的重复值

如何在min中不包含空值

React - 如何在包含函数中排除特定的数组值

在VBA中,如果数组包含值,如何在数组上进行迭代并用1填充某些单元格;如果不包含,则如何填充0?

如何在 Swift 中更新数组中包含的值?

如何在样本数组中查找重复值

如何在数组中查找并返回重复值

如何在Flutter中重复数组的值

如何在foreach循环中从数组中删除重复值?

如何在MongoDB中删除列表数组内的重复值?

如何在数组中返回非重复值?

如何在Codeigniter中删除重复值数组?

包含重复值的数组

如何在React Native中更新数组状态

如何在React中添加到状态数组

如何在React中从状态数组添加或删除项目

如何在React中从全局变量设置状态数组

如何在React中更改数组元素的状态

如何在 React Typescript 中定义处于状态的对象数组?