使用的typeError:使用发电机函数时元素是不确定的

发布于 2025-02-11 04:18:53 字数 851 浏览 0 评论 0原文

我有以下JavaScript片段:


/**
 *  Returns the maximal element in the `iterable` as calculated by the given function
 *  @param {Iterable} iterable - iterable to find the maximal element in
 *  @param {function} fn - function to calculate the maximal element
 * 
 *  @returns {any} - maximal element in the `iterable`
 */
function maxBy(iterable, fn) {
    let maximalElement = iterable[0];
    for (let element of iterable) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}


// example generator function
generator = function* ()  {
   yield [3,4]
   yield [4,6]
}
maxBy(generator(), element => element[1])

当我在浏览器控制台中运行此程序时,我会得到unduck typeError:element是未定义的错误,我似乎无法发现代码中的错误在哪里。

I have the following javascript snippet:


/**
 *  Returns the maximal element in the `iterable` as calculated by the given function
 *  @param {Iterable} iterable - iterable to find the maximal element in
 *  @param {function} fn - function to calculate the maximal element
 * 
 *  @returns {any} - maximal element in the `iterable`
 */
function maxBy(iterable, fn) {
    let maximalElement = iterable[0];
    for (let element of iterable) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}


// example generator function
generator = function* ()  {
   yield [3,4]
   yield [4,6]
}
maxBy(generator(), element => element[1])

When I run this in browser console, I get Uncaught TypeError: element is undefined error and I can't seem to spot where's the error in my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2025-02-18 04:18:53

您不能使用iToble [0]访问发电机中的第一个元素。您需要获取迭代器并进行迭代:

function maxBy(iterable, fn) {
    const iterator = iterable[Symbol.iterator]();
    const first = iterator.next();
    if (first.done) return;
    let maximalElement = first.value;
    for (let element of iterator) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;
}

或者,只需使用undefined初始化maximalelemt,如果iTable是空的,这可能会发生这种情况:

function maxBy(iterable, fn) {
    let maximalElement;
    for (let element of iterable) {
        if (maximalElement === undefined || fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}

You cannot use iterable[0] to access the first element in a generator. You'll need to get the iterator and iterate it:

function maxBy(iterable, fn) {
    const iterator = iterable[Symbol.iterator]();
    const first = iterator.next();
    if (first.done) return;
    let maximalElement = first.value;
    for (let element of iterator) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;
}

Alternatively, just initialise maximalElement with undefined, this might happen anyway if the iterable is empty:

function maxBy(iterable, fn) {
    let maximalElement;
    for (let element of iterable) {
        if (maximalElement === undefined || fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文