-2

I have a js array like this.

const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
 [ 20, 7, 23, 19, 10, 15, 8 ],
  [ 20, 7, 23, 19, 10, 15, 13 ],
  [ 20, 7, 23, 19, 10, 25, 8 ],
  [ 20, 7, 23, 19, 10, 25, 13 ],
  [ 20, 7, 23, 19, 10, 8, 13 ],
  [ 20, 7, 23, 19, 15, 25, 8 ],
  [ 20, 7, 23, 19, 15, 25, 13 ],
  [ 20, 7, 23, 19, 15, 8, 13 ],
  [ 20, 7, 23, 19, 25, 8, 13 ],
  [ 20, 7, 23, 10, 15, 25, 8 ],
  [ 20, 7, 23, 10, 15, 25, 13 ],
  [ 20, 7, 23, 10, 15, 8, 13 ],
  [ 20, 7, 23, 10, 25, 8, 13 ],
  [ 20, 7, 23, 15, 25, 8, 13 ],
  [ 20, 7, 19, 10, 15, 25, 8 ],
  [ 20, 7, 19, 10, 15, 25, 13 ],
  [ 20, 7, 19, 10, 15, 8, 13 ],
  [ 20, 7, 19, 10, 25, 8, 13 ],
  [ 20, 7, 19, 15, 25, 8, 13 ],
  [ 20, 7, 10, 15, 25, 8, 13 ],
  [ 20, 23, 19, 10, 15, 25, 8 ],
  [ 20, 23, 19, 10, 15, 25, 13 ],
  [ 20, 23, 19, 10, 15, 8, 13 ],
  [ 20, 23, 19, 10, 25, 8, 13 ],
  [ 20, 23, 19, 15, 25, 8, 13 ],
  [ 20, 23, 10, 15, 25, 8, 13 ],
  [ 20, 19, 10, 15, 25, 8, 13 ],
  [ 7, 23, 19, 10, 15, 25, 8 ],
  [ 7, 23, 19, 10, 15, 25, 13 ],
  [ 7, 23, 19, 10, 15, 8, 13 ],
  [ 7, 23, 19, 10, 25, 8, 13 ],
  [ 7, 23, 19, 15, 25, 8, 13 ],
  [ 7, 23, 10, 15, 25, 8, 13 ],
  [ 7, 19, 10, 15, 25, 8, 13 ],
  [ 23, 19, 10, 15, 25, 8, 13 ] ]

var combination_before = [ 20,7,23, 19, 10, 15, 25, 8, 13 ]; These are the results of choosing seven out of nine.

I would like to return some elements only when sum of the elements is 100.

how do I make reducer if condition?

horoyoi o
  • 398
  • 4
  • 18
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Nov 12 '18 at 11:21
  • ok I will not remove my attempt later.~ – horoyoi o Nov 12 '18 at 11:30

3 Answers3

2

you can use filter for filtering your array and reduce for sum your nested arrays, and check in your filter which array sum is equal to 100

const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
 [ 20, 7, 23, 19, 10, 15, 8 ],
  [ 20, 7, 23, 19, 10, 15, 13 ],
  [ 20, 7, 23, 19, 10, 25, 8 ],
  [ 20, 7, 23, 19, 10, 25, 13 ],
  [ 20, 7, 23, 19, 10, 8, 13 ],
  [ 20, 7, 23, 19, 15, 25, 8 ],
  [ 20, 7, 23, 19, 15, 25, 13 ],
  [ 20, 7, 23, 19, 15, 8, 13 ],
  [ 20, 7, 23, 19, 25, 8, 13 ],
  [ 20, 7, 23, 10, 15, 25, 8 ],
  [ 20, 7, 23, 10, 15, 25, 13 ],
  [ 20, 7, 23, 10, 15, 8, 13 ],
  [ 20, 7, 23, 10, 25, 8, 13 ],
  [ 20, 7, 23, 15, 25, 8, 13 ],
  [ 20, 7, 19, 10, 15, 25, 8 ],
  [ 20, 7, 19, 10, 15, 25, 13 ],
  [ 20, 7, 19, 10, 15, 8, 13 ],
  [ 20, 7, 19, 10, 25, 8, 13 ],
  [ 20, 7, 19, 15, 25, 8, 13 ],
  [ 20, 7, 10, 15, 25, 8, 13 ],
  [ 20, 23, 19, 10, 15, 25, 8 ],
  [ 20, 23, 19, 10, 15, 25, 13 ],
  [ 20, 23, 19, 10, 15, 8, 13 ],
  [ 20, 23, 19, 10, 25, 8, 13 ],
  [ 20, 23, 19, 15, 25, 8, 13 ],
  [ 20, 23, 10, 15, 25, 8, 13 ],
  [ 20, 19, 10, 15, 25, 8, 13 ],
  [ 7, 23, 19, 10, 15, 25, 8 ],
  [ 7, 23, 19, 10, 15, 25, 13 ],
  [ 7, 23, 19, 10, 15, 8, 13 ],
  [ 7, 23, 19, 10, 25, 8, 13 ],
  [ 7, 23, 19, 15, 25, 8, 13 ],
  [ 7, 23, 10, 15, 25, 8, 13 ],
  [ 7, 19, 10, 15, 25, 8, 13 ],
  [ 23, 19, 10, 15, 25, 8, 13 ] ];
  
  const result =  test_arr.filter(arr => arr.reduce((a, b) => a + b, 0) == 100);
  
  console.log(result);
Artyom Amiryan
  • 2,478
  • 1
  • 7
  • 18
0

const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
 [ 20, 7, 23, 19, 10, 15, 8 ],
  [ 20, 7, 23, 19, 10, 15, 13 ],
  [ 20, 7, 23, 19, 10, 25, 8 ],
  [ 20, 7, 23, 19, 10, 25, 13 ],
  [ 20, 7, 23, 19, 10, 8, 13 ],
  [ 20, 7, 23, 19, 15, 25, 8 ],
  [ 20, 7, 23, 19, 15, 25, 13 ],
  [ 20, 7, 23, 19, 15, 8, 13 ],
  [ 20, 7, 23, 19, 25, 8, 13 ],
  [ 20, 7, 23, 10, 15, 25, 8 ],
  [ 20, 7, 23, 10, 15, 25, 13 ],
  [ 20, 7, 23, 10, 15, 8, 13 ],
  [ 20, 7, 23, 10, 25, 8, 13 ],
  [ 20, 7, 23, 15, 25, 8, 13 ],
  [ 20, 7, 19, 10, 15, 25, 8 ],
  [ 20, 7, 19, 10, 15, 25, 13 ],
  [ 20, 7, 19, 10, 15, 8, 13 ],
  [ 20, 7, 19, 10, 25, 8, 13 ],
  [ 20, 7, 19, 15, 25, 8, 13 ],
  [ 20, 7, 10, 15, 25, 8, 13 ],
  [ 20, 23, 19, 10, 15, 25, 8 ],
  [ 20, 23, 19, 10, 15, 25, 13 ],
  [ 20, 23, 19, 10, 15, 8, 13 ],
  [ 20, 23, 19, 10, 25, 8, 13 ],
  [ 20, 23, 19, 15, 25, 8, 13 ],
  [ 20, 23, 10, 15, 25, 8, 13 ],
  [ 20, 19, 10, 15, 25, 8, 13 ],
  [ 7, 23, 19, 10, 15, 25, 8 ],
  [ 7, 23, 19, 10, 15, 25, 13 ],
  [ 7, 23, 19, 10, 15, 8, 13 ],
  [ 7, 23, 19, 10, 25, 8, 13 ],
  [ 7, 23, 19, 15, 25, 8, 13 ],
  [ 7, 23, 10, 15, 25, 8, 13 ],
  [ 7, 19, 10, 15, 25, 8, 13 ],
  [ 23, 19, 10, 15, 25, 8, 13 ] ];
  
  var resultArr = [];
  
  for (var i = 0; i < test_arr.length; i++){
  temp = test_arr[i].reduce(getSum)
    if (temp == 100) {
    resultArr.push(test_arr[i]);
  }
  }
  
  function getSum(total, num) {
    return total + num;
}
  
  
  console.log(resultArr)
Wimanicesir
  • 3,428
  • 2
  • 9
  • 23
0

You could take a different approach at the moment, where you take all combination and return only the ones who match the conditions by length and sum.

function getCombinations(array, length, sum) {
    function iter(array, temp) {
        if (temp.length === length) {
            if (temp.reduce((a, b) => a + b) === sum) {
                result.push(temp);
            }
            return;
        }
        if (!array.length || array.length + temp.length < length) {
            return;
        }
        iter(array.slice(1), temp.concat(array[0]));
        iter(array.slice(1), temp);
    }

    var result = [];    
    iter(array, []);
    return result;
}

var array = [20, 7, 23, 19, 10, 15, 25, 8, 13];

console.log(getCombinations(array, 7, 100));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324