How to get the remaining interval in an array by js

arr = [
    [500,1000],
    [2000,4000],
    [5000,8000],
    [8000,9000],
    [10000,12000],
    [14000,20000],
    [23000,30000],
]

the range of intervals represented by the known array arr [500 ~ (1, 000)]

1 the same parts should be merged into 5000-9000

.

2 find the residual interval of the residual space, for example, 0500 1000-2000 4000-5000. Wait

Mar.12,2021

    const arr = [
        [500,1000],
        [2000,4000],
        [5000,8000],
        [8000,9000],
        [10000,12000],
        [14000,20000],
        [23000,30000],
    ];
        let newArr = arr.reduce((a,v)=>{
        if(typeof(a[0])=="number"){
          if(a[1] == v[0]) return [a[0],v[1]]
          //console.log([a,v])
          return [a,v]
        }else{
          if(a[a.length-1][1]!=v[0]) { 
            a.push(v)
          }else{ 
            a[a.length-1][1] = v[1]
          }
          return a
        }
        })
    console.log(newArr) 
    let resArr = newArr.reduce((a,v)=>{
      if(typeof(a[0])=="number"){
        if(a[0]!=0) return [[0,a[0]],[a[1],v[0]],[v[1],v[1]]]
        return [[a[1],v[0]],[v[1],v[1]]] 
      }else{ 
        a[a.length-1][1]  = v[0];
        if(a.length!=newArr.length) a.push([v[1],v[1]])
      }
      return a 
    })
    console.log(resArr)
Menu