SOURCE

var twoSum = function(nums,target){
    const map = new Map()
    nums.forEach((item,i)=>{
        let cha = target-item
        if(map.has(cha)){
            return [map.get(cha),i]
        }else{  
            map.set(item,i)
        }
    })
}

//并不是所有的循环都可以用forEach
// ________

var twoSum2 = function(nums,target){
    const map = new Map()
    for(let i=0;i<nums.length;i++){
        let cha = target-nums[i]
         console.log('cha',cha,i)
        if(map.has(cha)){
            console.log(1)
            return [map.get(cha),i]
        }else{
            console.log(2)
            map.set(nums[i],i)
        }
    }
}

let nums = [15,6,1,3]
let target = 9
console.log(twoSum(nums,target))
console.log(twoSum2(nums,target))
console 命令行工具 X clear

                    
>
console