学习到了 new Set()的去重功能,基本用法

    let arr = [
      {
        label: 123,
      },
      {
        label: 321,
      },
      {
        label: 321,
      },
    ]
    let newArr = arr.map(item => item.label)
    let fillter = Array.from(new Set([...newArr])) // [123,321]

更多用法 参考:这里
当然 最终解决方案并不是通过new Set()

ADD_TAG: (state, action) => {
      state.tag = action
      setStore({ name: 'tag', content: state.tag })
      if (state.tagList.some(ele => diff(ele, action))) return
      // state.tagList 是个 数组对象
      // 重点在这里,通过 找到重复的下标,然后删除
      let index = state.tagList.findIndex(item => item.label === action.label)
      if (index !== -1) {
        state.tagList.splice(index, 1)
      }
      
      state.tagList.push(action)
      setFistTag(state.tagList)
      setStore({ name: 'tagList', content: state.tagList })
    },
Logo

前往低代码交流专区

更多推荐