gpt4 book ai didi

javascript - 一些 lodash 函数未定义

转载 作者:行者123 更新时间:2023-12-01 02:04:47 25 4
gpt4 key购买 nike

某些 lodash 函数给出“不是函数错误”。例如:_.minBy_.maxBy_.cloneDeep等。

其他功能如_.map_.clone等都工作正常。我尝试过 lodash 4 和 lodash 3。

这是我的代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.1.0/lodash.js"></script>
<script type="text/javascript">
var drinks = [
{ 'name': 'Coke', 'quantity': 2 },
{ 'name': 'Red Bull', 'quantity': 6 }
];
var currentDrinks = _.map(drinks, 'name');
console.log(currentDrinks);
// → [‘Coke’, ‘Red Bull’]
var a = [{"type":"exam","score":47.67196715489599},{"type":"quiz","score":41.55743490493954},{"type":"homework","score":70.4612811769744},{"type":"homework","score":48.60803337116214}];

const b = _.minBy(a, 'score');
console.log(b);

</script>

此处 _.map 有效,而 _.minBy 无效。我应该使用哪个版本的 lodash 来支持 _.minBy_.deepClone

最佳答案

它与 lodash 4 完美配合:

var drinks = [
{ 'name': 'Coke', 'quantity': 2 },
{ 'name': 'Red Bull', 'quantity': 6 }
];
var currentDrinks = _.map(drinks, 'name');
console.log(currentDrinks); // → [‘Coke’, ‘Red Bull’]
var a = [
{"type":"exam","score":47.67196715489599},
{"type":"quiz","score":41.55743490493954},
{"type":"homework","score":70.4612811769744},
{"type":"homework","score":48.60803337116214}
];
const b = _.minBy(a, 'score');
console.log(b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>

.minBy.maxBy 已在 4.0.0 版本中引入。只需使用比此更高的版本即可。

编辑:在您上传的文件中,您没有指定整个库。您只是加载核心。 (在您的问题中,您引用了正确的文件,但版本错误。)

更改:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script>

至:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

关于javascript - 一些 lodash 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com