我们需要编写一个包含两个数组的JavaScript函数,例如arr1和arr2。我们的函数应按arr1的字符串的字典顺序返回排序的数组,这些字符串是arr2的字符串的子字符串。
示例
为此的代码将是-const lexicographicalSort = (arr1 = [], arr2 = []) => {
let i, j;
const res = [];
outer: for (j = 0; j
for (i = 0; i
if (arr2[i].includes(arr1[j])) {
res.push(arr1[j]);
continue outer;
};
};
}
return res.sort();
};
const arr2 = ["lively", "alive", "harp", "sharp", "armstrong"];
const arr1 = ["xyz", "live", "strong"];
console.log(lexicographicalSort(arr1, arr2));
输出结果
控制台中的输出将是-[ 'live', 'strong' ]