javascript - 如何对循环的结果进行循环
问题描述
我有一个返回对象数组的函数。
const matches = checkMatching(param1, param2, param3);
函数 checkMatching 返回数组,我想在该数组上调用另一个函数 checkMatching。然后,如果 checkMatchingfunction 仍然返回数组,我想在该数组上调用 checkMatching。总结我想创建无限循环,直到 chechMatching 不会返回数组;
基本上我正在尝试创建数组
let allMatchingWords = []
它从循环中收集所有结果。
目前我有这样的东西
const checkMatching = (board, r, c) => {
const top = board[r - 1] !== undefined && { row: r - 1, column: c };
const bottom = board[r + 1] !== undefined && { row: r + 1, column: c };
const left = board[r][c - 1] !== undefined && { row: r, column: c - 1 };
const right = board[r][c + 1] !== undefined && { row: r, column: c + 1 };
const directionsWithMatches = [top, bottom, left, right]
.filter(dir => dir instanceof Object)
.filter(({ row, column }) => board[row][column].word === board[r][c].word);
return directionsWithMatches;
};
const matches = checkMatching(param1, param2, param3);
let allMatchingWords = [];
matches.map(({ row, column }) => {
allMatchingWords = [
...matches,
...allMatchingWords,
...checkMatching(param1, param2, param3),
];
});
/// removing duplicates
allMatchingWords = allMatchingWords.filter(
(v, i, a) => a.findIndex(t => t.key1 === v.key1 && t.key2 === v.key2) === i,
);
但是现在有一个循环两次的捷径。
我试图创建 while 循环,但我不确定这是否可行。
解决方案
推荐阅读
- matlab - “错误使用/矩阵尺寸必须一致。” 创建功能系统时
- javascript - 如何使网页上的所有内容都无法点击,直到使用 Javascript 加载某些内容
- oauth-2.0 - ClientCredentials 流访问令牌是否仅限于特定租户?
- python - 检测到的物体上的地标,以确定它是否正面朝上,以确定正确的旋转
- android - Webview 的 LoadURL 在 android 10 上不起作用
- javascript - 如何使用“docxtemplater”生成的多个 docx 文件创建一个 zip 文件?
- arrays - 如何更直接地生成具有相同元素的数组?
- c# - 在 datagridview 循环中创建线程并保留该行
- android - Android Studio 4.1 插件错误:插件“Parcelable Code Generator(for kotlin)”不兼容(仅在 IntelliJ IDEA 中支持)
- regex - 我需要一个只检索字母字符的表达式