Finding all pairs of elements in an array that sum to a target value Is there a shorter way and does not involve nested for loops?

const checkIfSumFromTwoNumbers = (arrayOfNum, targetValue) =&gt; {
    let found = [];
    let result = [];

    for (let value of arrayOfNum) {
        if (found[targetValue - value] === true ) {
            result.push({[arrayOfNum.indexOf(value)]: value, [arrayOfNum.indexOf(targetValue-value)]: targetValue - value});
        }
        found[value] = true;
    }
    return result;
};
No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) =&gt; 
  arrayOfNums.reduce((matches, val) =&gt; {
      const target = arrayOfNums.find(x =&gt; (val + x) === targetValue);
      target &amp;&amp; matches.push([val, target]);
      return matches;
   }, [])




I was actually ignorant to the fact your code actually works which means it can be done in one pass :) here's a slightly neater / more concise version of what you already have:

const checkIfSumFromTwoNumbers = (arrayOfNum, targetValue) =&gt; {
  const seen = {};
  return arrayOfNum.reduce((matches, val) =&gt; {
    const delta = targetValue - val;
    seen[delta] &amp;&amp; matches.push([delta, val]);
    seen[val] = true;
    return matches;
  }, []);
};