简体   繁体   English

在 Node.js 模块中使用函数

[英]Using function within Node.js module

I'm having trouble using a function within a Node.js module.我在 Node.js 模块中使用函数时遇到问题。 I'm using a custom sort function to sort an array of objects by the value of a certain property.我正在使用自定义排序函数按某个属性的值对对象数组进行排序。

exports.getResult = function(cards) {
    cards.sort(sortByField('suit'));
    ...
    ...
    ...
    return cards;
}

function sortByField(fieldName) {
    return function(card1, card2) {
        return card1[fieldName] < card2[fieldName]
    };
}

When I use the getResult function NOT as an export, and call it from the same file, everything works as expected.当我使用 getResult 函数 NOT 作为导出,并从同一个文件中调用它时,一切都按预期工作。 The card object with the highest suit value is first and the card object with the lowest value is last.花色值最高的卡片对象在前,花色值最低的卡片对象在后。

However, when I call the function as an export from my index.js, the sort doesn't happen.但是,当我将该函数作为 index.js 的导出调用时,排序不会发生。 There is no error, the rest of the function just executes with the array in the same order that it was before the sort function.没有错误,函数的其余部分只是按照与排序函数之前相同的顺序对数组执行。

I've tried everything I can think of.我已经尝试了所有我能想到的。 I've done module.exports.sortByField(), I've tried requiring the .sortByField() in a completely separate module.我已经完成了 module.exports.sortByField(),我尝试在一个完全独立的模块中要求 .sortByField()。 I'm sure there's a simple answer, but I just can't figure it out.我敢肯定有一个简单的答案,但我就是想不通。

I suspect that it's working the same in both cases, but the problem is that the sort callback is incorrect.我怀疑它在两种情况下的工作方式相同,但问题是sort回调不正确。

A sort callback must return a negative number, 0, or a positive number, not a boolean: A negative number if the first entry should be before the second, 0 if they're the same, a positive number if the first should come after the second. sort回调必须返回负数、0 或正数,而不是布尔值:如果第一个条目应该在第二个条目之前,则返回负数,如果它们相同,则返回 0,如果第一个条目应该在后面,则返回正数第二。

If cards contains numbers, you want:如果cards包含数字,您需要:

return card1[fieldName] - card2[fieldName];

...if you want to sort ascending. ...如果你想升序排序。

If it's strings, you can use localeCompare :如果是字符串,则可以使用localeCompare

return card1[fieldName].localeCompare(card1[fieldName]);

So I hope I can be forgiven for being new to all this, but it turns out that the code I posted originally wasn't the problem at all.所以我希望可以原谅我对这一切的陌生,但事实证明,我最初发布的代码根本不是问题。 Long story short, the array that I passed to the getResult() function was an array of arrays, instead of an array of objects.长话短说,我传递给 getResult() 函数的数组是一个数组数组,而不是一个对象数组。

I was adding cards to an array of played cards with this code:我正在使用以下代码将卡片添加到一系列已播放的卡片中:

playedCards.push(game.playCard(1, players[0].hand, 3));

and the playCard() function was exporting an array:而 playCard() 函数正在导出一个数组:

exports.playCard = function(amount, hand, index) {
    const cards = hand.splice(index, amount);
    return cards;
};

but I should have been exporting an individual object:但我应该一直导出单个对象:

exports.playCard = function(amount, hand, index) {
    const cards = hand.splice(index, amount);
    return cards[0];
};

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

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