Given a list of numbers const arr = [{ key1: 10 }, { key2: 5 }, { key3: 7 }, { key4: 17 }];
and a number k say const k = 17;
, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k = 17, we should return True, since 10 + 7 =17.
Wrote below code using key,values
in Map,but seems not able to get the solution,can anyone suggest what code change is needed below ,want to solve especially using Maps
const arr = [{ key1: 10 }, { key2: 5 }, { key3: 7 }, { key4: 17 }];
const k = 17;
let addInput = new Map(arr.flatMap((x) => Object.entries(x)));
addInput.forEach((v) => {
for (var i = 0; i < addInput.size; i++) {
if (v != addInput[i]) {
if (addInput[i] + v == k) {
console.log(`${v} + ${addInput[i]} = ${k} (true)`);
}
}
}
});