数组和的数组,如果满足条件则中断

时间:2018-11-12 11:14:52

标签: javascript arrays if-statement reducers

我有一个这样的js数组。

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 ]; 这些是从九个中选择七个的结果。

仅当元素之和为100时,我才想返回一些元素。

如果有条件怎么做减速器?

3 个答案:

答案 0 :(得分:2)

您可以使用filter来过滤数组,并使用reduce来对嵌套数组求和,并在过滤器中检查哪个数组和等于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);

答案 1 :(得分: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)

答案 2 :(得分:0)

此刻您可以采用另一种方法,将所有组合组合起来,只返回符合条件的长度和总和。

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; }