JS数据处理实战(四)

<html>
	<head>
		<title>js数据处理实战(四)</title>
	</head>
	<body>
    <script>
      // node id 相同的,最后一个 node id 的 method === 'wait'的,删除之前的 所有 item,只保留最后一个
      // 如果最后一个不是wait, 只删除所有wait的
      // 已有数据
      let arr = [
        { node_id: 1, method: 'wait' },
        { node_id: 2, method: 'wait' },
        { node_id: 1, method: 'wait' },
        { node_id: 4, method: 'complete' },
        { node_id: 5, method: 'wait' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'complete' },
        { node_id: 3, method: 'complete' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'complete' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'wait' },
        { node_id: 3, method: 'complete' },
        { node_id: 1, method: 'wait' },
        { node_id: 5, method: 'complete' }
      ]
      // 数据结果应为
      // node_id为1 最后是wait, 删除之前所有 id 为 1 的 wait, 只保留最后一个wait
      // node_id为2 最后是wait, 删除之前所有 id 为 2 的 wait, 只保留最后一个wait
      // node_id为3 最后不是wait, 之前也没有wait,所以都保留不动
      // node_id为4 最后不是wait, 之前也没有wait,所以都保留不动
      // node_id为5 最后不是wait, 删除所有 id 为 5 的 wait, 一个wait都不留
      let arr2 = [
        { node_id: 4, method: 'complete' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'complete' },
        { node_id: 3, method: 'complete' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'complete' },
        { node_id: 1, method: 'complete' },
        { node_id: 2, method: 'wait' },
        { node_id: 3, method: 'complete' },
        { node_id: 1, method: 'wait' },
        { node_id: 5, method: 'complete' }
      ]
      console.log('arr2.length', arr.length)
      console.log('arr3.length', arr2.length)
      console.log(disposeData(arr))
      function disposeData (arr) {
        // 定义最终结果数组
        let result = []
        // 先按 node_id 分组
        const groupAllData = {}
        arr.forEach((item, index) => {
          if (groupAllData.hasOwnProperty(item.node_id)) {
            groupAllData[item.node_id]['data'].push(item)
            groupAllData[item.node_id]['idx'].push(index)
          } else {
            groupAllData[item.node_id] = {
              data: [item],
              idx: [index]
            }
          }
        })
        console.log('groupAllData1', groupAllData)
        // 判断每一个分组最后一个的 method 是否为 wait, 并记录每一个在原数组中对应的 index
        for (const key in groupAllData) {
          const data = groupAllData[key].data
          const idx = groupAllData[key].idx
          groupAllData[key].lasetIsWait = data[data.length - 1].method === 'wait'
          groupAllData[key].lasetIsWaitIndex = idx[idx.length - 1]
        }
        console.log('groupAllData2', groupAllData)
        // 从原数组中筛选出 符合条件的
        result = arr.filter((item, index) => {
          if (item.method === 'wait') {
            // 如果当前 id 的最后一个的 method 是 wait
            if (groupAllData[item.node_id].lasetIsWait) {
              // 只返回最后一个
              return index === groupAllData[item.node_id].lasetIsWaitIndex
            } else {
              return false
            }
          } else {
            return true
          }
        })
        console.log('result', result)
      }
    </script>
	</body>
</html>
发布了119 篇原创文章 · 获赞 74 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/q95548854/article/details/103906606