let arr = ['1.1', '1.2', '1.3','2.1', '2.1', '3.1']
let result = []
while(arr.length > 0){
let par = {}
let key = 0let 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', '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);
谢谢大家回答,非常感谢 我要这样[{0:"1.1"},{0:"1.2"},{0:"1.3"},{0:"2.1",1:"2.1"},{0:"3.1"}]
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)
var arr3 = []
arr2.forEach((item)=>{
var obj = {}
//判断这个数组的长度大于1
if(item.length > 1){
var i=0;
while (i<item.length)
{
obj[`${i}`] = item[0]
i++;
}
arr3.push(obj)
}
// 长度为1 即下标为1
else{
obj[`0`] = item[0]
arr3.push(obj)
}
})
console.log(arr3)
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)
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)