<script>
var arr = [1, 5, 8, 9, 10, 7]
const json = {}
const result = 15
for (var i = 0; i < arr.length; i++) {
const target = result - arr[i]
if (json[target]) {
console.log(arr[json[target]], arr[i])
} else {
json[arr[i]] = [i]
}
}
</script>
找出符合的下标
<script>
var arr = [10, 2, 3, 5, 9, 7, 6]
var n = 15
var res = []
for (var i in arr) {
var k = n - arr[i]
if (arr.indexOf(k) !== -1) {
res.push(i)
}
}
console.log(res)
</script>