一位数组变成json格式?
发布于 6 年前 作者 xiulan07 7040 次浏览 来自 问答

把上面数据做成这样的

[{"0”:“1.1”},{"0”:1.2},{"0”:1.3},{"0”:2.1,“1”:2.1},{"0”:3.1}]

6 回复

集思广益互相帮助,感谢对社区的支持

let arr = ['1.1', '1.2', '1.3','2.1', '2.1', '3.1']
    let result = []
    while(arr.length > 0){
      let par = {}
      let key = 0
      let value = arr[0]
      while(arr.length > 0 ){
        let res = arr.indexOf(value)
        if( res > -1 ){
          par[''+key] = value
          key++
          arr.splice(res,1)
        }else{
          break
        }
      }
      result.push(par)
    }
    console.log('result->',result);

    let arr = ["1.1","1.2","1.3","2.1","2.1","2.3"];
    let arr1 = [];
    let arr2 = [];
    for(let i=0;i<arr.length;i++){
      console.log(arr1.indexOf(arr[i]))
      
      if(arr1.indexOf(arr[i])>-1){
        arr2[arr1.indexOf(arr[i])].push(arr[i])
      }else{
        let newArr = []
        newArr.push(arr[i])
        arr1.push(arr[i])
        arr2.push(newArr)
      }
    }
    console.log(arr2)
let arr = ["1.1","1.2","1.3","2.1","2.2","2.3"];
let newArr = [];
for(let i=0;i<arr.length;i++){
    newArr.push({
        "0":arr[i]
    })
}
console.log(newArr)

let arr1 = ["1.1","1.2","1.3","2.1","2.2","2.3"];
let arr2 = arr1.map(m => {
      return {0:m};
    })

console.log(arr2)

谢谢大家回答,非常感谢 我要这样[{0:"1.1"},{0:"1.2"},{0:"1.3"},{0:"2.1",1:"2.1"},{0:"3.1"}]

回到顶部