我希望在下面的数组中找到x数是否存在。为了在里面找到x数,数组中的任何元素都可以得到给定的x数。这是基于元素的有一个内部数组并检查它是否存在。这不像是升职和合并。
let arrr = [
4,
7,
6,
2,
7,
6
];发布于 2019-05-27 05:23:01
它没有被优化,但它有效:
function Contains(array, value) {
const str = array.reduce((p, c) => p + c.toString(), "");
const fn = function (active, rest, a) {
const sum = rest.split("").reduce((p, c) => p + Number(c), 0);
if (sum === value) return true;
if (!active && !rest) return;
if (!rest) {
a.push(active);
} else {
if (fn(active + rest[0], rest.slice(1), a) === true) return true;
if (fn(active, rest.slice(1), a) === true) return true;
}
return a;
}
const result = fn("", str, []) === true;
return result === true;
}
Contains(array, value)https://stackoverflow.com/questions/56319055
复制相似问题