1 回答
已采纳
狂飙的蜗牛_1
TA贡献96条经验 获得超43个赞
//可以这样写 var arr = ["a","b","a","b","b"]; var map = {}; for(var i = 0;i<arr.length;i++){ if(typeof map[arr[i]] === 'number'){ map[arr[i]]++; }else{ map[arr[i]] = 0; } if(map[arr[i]] !== 0){ arr[i] = arr[i] + "_" + map[arr[i]]; } } console.log(arr); //["a", "b", "a_1", "b_1", "b_2"]
添加回答
举报
0/150
提交
取消