编辑代码

function sum (a, target) {
    const sum = {} 
    let diffIndex = null
    for (let key in a) {
        const diff = target - a[key]
        if (key) {
            diffIndex = Object.values(sum).findIndex(val => val === diff)
        }
        if (!diffIndex) {
            sum[key] = target - a[key]
        } else {
            return [key, diffIndex]
        }
    }
    // console.log(sum)
}

console.log(sum([2, 7, 11, 15], 9))
// console.log(Object.values({}))
// console.log([].findIndex(1))

// let obj = { name: 1, age: 2 }
// console.log(Object.values(obj))
// const arr = Object.values(obj)
// console.log(arr.findIndex(val => val === 1))