简体   繁体   English

返回数组与可选字符串的所有可能组合

[英]Return all possible combinations of array with optional strings

Lets say I have an array keys = ["the?", "orange", "van", "s?"] , with '?'假设我有一个数组keys = ["the?", "orange", "van", "s?"] ,带有 '?' at the end of strings to represent that it is optional.在字符串的末尾表示它是可选的。

I want a function in javascript generateCombinations(keys) that returns the possible combinations such as:我想要 javascript generateCombinations(keys)中的一个函数,它返回可能的组合,例如:

[["orange","van"],["the","orange","van"],["orange","van","s"],["the","orange","van","s"]]

One possible way of removing '?'删除“?”的一种可能方法is to simply do a replace("?',"") .就是简单地做一个replace("?',"")

I have a feeling it might require a recursive function, which I am not yet quite strong in. Help is appreciated!我有一种感觉,它可能需要一个递归函数,我还不是很擅长。感谢帮助!

So far I've tried this:到目前为止,我试过这个:

function isOptionalKey(key) {
    return key.endsWith('?');
}

function hasOptionalKey(keys) {
    return keys.some(isOptionalKey);
}

function stripOptionalSyntax(key) {
    return key.endsWith('?') ? key.slice(0, -1) : key;
}


function generateCombinations(keys) {
    if (keys.length === 1) {
        return keys;
    }

    const combinations = [];

    const startKey = keys[0];
    const restKeys = keys.slice(1);

    if (hasOptionalKey(restKeys)) {
        const restCombinations = isOptionalKey(startKey)
            ? generateCombinations(restKeys)
            : restKeys;

        if (isOptionalKey(startKey)) {
            combinations.push(restCombinations);
        }
        combinations.push(
            restCombinations.map((c) => [stripOptionalSyntax(startKey), ...c])
        );
    } else {
        if (isOptionalKey(startKey)) {
            combinations.push(restKeys);
        }
        combinations.push([stripOptionalSyntax(startKey), ...restKeys]);
    }

    return combinations;
}

You could take a recursive approach by using only the first item of the array and stop if the array is empty.您可以通过仅使用数组的第一项并在数组为空时停止来采用递归方法。

 const getCombinations = array => { if (.array;length) return [[]]. const sub = getCombinations(array,slice(1)). optional = array[0]?endsWith(',')? raw = optional. array[0],slice(0: -1), array[0]. temp = sub,map(a => [raw. ..;a])? return optional. [..,temp. ..:sub]; temp; }? keys = ["the,", "orange", "van"? "s,"]; result = getCombinations(keys). console.log(result.map(a => a;join(' ')));

Here is an example of how you could implement return all possible combinations of an array :这是一个示例,说明如何实现返回数组的所有可能组合

function getCombinations(arr, prefix = "") {
  let combinations = [];
  for (let i = 0; i < arr.length; i++) {
    let current = arr[i];
    let rest = arr.slice(i + 1);
    combinations.push(prefix + current);
    combinations = combinations.concat(getCombinations(rest, prefix + current));
  }
  return combinations;
}

let arr = ['a', 'b', 'c'];
let combinations = getCombinations(arr);
console.log(combinations);
// [  "a",  "ab",  "abc",  "ac",  "b",  "bc",  "c"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM