强大的javascript splice()

本文介绍如何利用JavaScript的splice()方法在Echarts图表数据回显场景中进行数据替换和处理。通过遍历和比较新旧数据长度,实现对Echarts配置中的series.data和相关选项进行动态更新,确保数据正确显示。涉及到的关键操作包括删除、插入和替换数组元素。
摘要由CSDN通过智能技术生成

javascript splice()可以用于插入、删除或替换数组的元素。


1.删除-用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数)
2.插入-向数组指定位置插入任意项元素。三个参数,第一个参数(其实位置),第二个参数(0),第三个参数(插入的项)
3.替换-向数组指定位置插入任意项元素,同时删除任意数量的项,三个参数。第一个参数(起始位置),第二个参数(删除的项数),第三个参数(插入任意数量的项)

下面是一段用到splice()的代码:

背景:对Echarts图表进行数据回显,由于之前踩过各种坑发现echars中的配置不能通过直接用新数组赋值实现默认数据的替换,只能循环给每个默认数据替换属性值,于是最后有下面这么一段冗长的代码。

            // 饼图name数组
            let graphDataPieX = JSON.parse(JSON.stringify(this.graphDataX))
            // 饼图value数组
            let graphDataPieY = JSON.parse(JSON.stringify(this.graphDataY))  
            // 处理data数组
            let inputColor = this.chartInfo.series[0].other
            let opacity = this.chartInfo.series[0].opacity
            let defaultColor = this.chartData.options.color
            let defa = this.chartInfo.series[0].data
            let defaLength = defa.length
            graphDataPieX.forEach((item, index) => {
              // 随机生成颜色
              let color = '#' + Math.random().toString(16).substr(2, 6).toUpperCase()
              // 比较新旧数据的长度,先删除原数组多出来的部分,再替换
              if (graphDataPieX.length <= defaLength) {
                // 对data处理
                defa.splice(graphDataPieX.length, defaLength - graphDataPieX.length)
                defa[index].name = item
                defa[index].value = graphDataPieY[0][index]
                defa[index].itemStyle.color = color
                // 对options中的color处理
                defaultColor.splice(graphDataPieX.length, defaLength - graphDataPieX.length)
                defaultColor[index] = color
                // 对透明度初始值处理
                opacity.splice(graphDataPieX.length, defaLength - graphDataPieX.length)
                opacity[index] = 100
                // 对颜色输入框的默认值处理
                inputColor.splice(graphDataPieX.length, defaLength - graphDataPieX.length)
                color = color.replace('#', '')
                inputColor[index] = color
              } else {
                // 先把原来data中存在的替换掉,再push多余的数据
                if (defaLength <= index) {
                  // data
                  defa.push({
                    name: item,
                    value: graphDataPieY[0][index],
                    itemStyle: { color: color },
                  })
                  // options中的color
                  defaultColor.push(color)
                  // 透明度初始值
                  opacity.push(100)
                  // 颜色输入框的默认值
                  color = color.replace('#', '')
                  inputColor.push(color)
                } else {
                  // data处理
                  defa[index].name = item
                  defa[index].value = graphDataPieY[0][index]
                  defa[index].itemStyle.color = color
                  // options中的color
                  defaultColor[index] = color
                  // 透明度初始值
                  opacity[index] = 100
                  // 颜色输入框的默认值
                  color = color.replace('#', '')
                  inputColor[index] = color
                }
              }
            })
            this.setChart(this.chartInfo)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值