一位数组变成json格式?
把上面数据做成这样的
[{"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)