js数组去除重复的对象

我们经常会碰到js当中有很多重复的对象的情况,尤其在react中碰到这种问题总是会报key重复的错误,对此,我们可以这么来解决这个问题。

        const hash = {};
          const filterValue = trades.reduce((item, next) => {
            const n = next;
            if (!hash[n.base_unit_code]) {
              hash[n.base_unit_code] = true && item.push({
                text: next.base_unit_code,
                value: next.base_unit_code,
              });
            }
            return item;
          }, []);
          this.setState({ filterValue });

以上是解决此类问题的正确思路以及解决办法。

你可能感兴趣的:(js数组去除重复的对象)