如何在JavaScript中自定义Array.reduce()中重写Array.every()? [英] How to rewrite an Array.every() in a custom Array.reduce() in javascript?
问题描述
任何列表高级列表转换都可以写在
Array.reduce()
音频/视频参考: https://youtu.be/Wl98eZpkp-c?t=138 。
问题:
纯粹为了学习如何重写 Array.every()
与 Array.reduce()
此问题源于我以前的问题< a href =https://stackoverflow.com/questions/40145757/are-there-any-higher-order-function-to-return-an-object-from-an-array-of-objects> here
Javascript示例:
var approved1 = [
{
dateApproved:new Date(),
id:1,
},
{
dateApproved:new Date(),
id:2,
}
];
var approved2 = [
{
dateApproved:null,
id:1,
},
{
dateApproved:new Date(),
id:2,
}
];
approved1every(a => a.dateApproved!= null)? 'APPROVED':'PENDING'
//'APPROVED'
approved2.reduce(every)? 'APPROVED':'PENDING'
//'PENDING'
我努力与我在哪里存储当前传递的值。我在哪里存储传递的值,如 Array.every()
?
function every(previousValue,currentValue,currentIdx,arr){
if(previousValue.dateApproved!== null& ;& currentValue.dateApproved!== null){
return currentValue;
}
}
你可以使用减少
而不是每个
并使其正常工作,但我建议您在适当的地方使用它们。两者都有独特的要求。
array.reduce
array.reduce (callback(curentElement,nextElement,index,array),[initialCurrentElement])
Array.reduce有4 args。
- currentElement :默认情况下,这将是第一次迭代的数组中的第一个元素,然后变量将保存您返回的值。如果一个初始值被传递,那么它将保持该值并从那里开始。
- nextElement :默认情况下它包含第二个或下一个元素。如果初始值通过,这将保存第一个值。
- 索引:此选项保存当前元素的索引。
- 数组:这是我们循环的父数组。
- initialCurrentElement :这是一个可选参数。如果这是通过的,循环开始于此。
以下是一个示例:
注意:
-
Array.every
将首先破坏falsey
条件。 Array.reduce不会。 -
Array.reduce
旨在比较相同数组的2个值,其中Array.every
旨在将每个值与表达式进行比较。使用.reduce
而不是.every
只是一个过分的。
var approved2 = [{dateApproved:null,id:1,} ,{dateApproved:new Date(),id:2,}]; var everyResult = approved2.every(x => {console.log(x.dateApproved)x.dateApproved!== null})console.log(everyResult )var reduceResult = approved2.reduce((p,c)=> {console.log(c.dateApproved)return!p?p:c.dateApproved!== null},true)console.log(reduceResult? ':'拒绝')
This question is for purely for learning purposes. funfunfunction on youtube says that
Any list higher order list transformation can be written in
Array.reduce()
Audio/video Reference: https://youtu.be/Wl98eZpkp-c?t=138.
Question:
Purely for learning how would one rewrite Array.every()
with Array.reduce()
This question stems from my previous question here
Javascript Example:
var approved1 = [
{
dateApproved: new Date(),
id: 1,
},
{
dateApproved: new Date(),
id: 2,
}
];
var approved2 = [
{
dateApproved: null,
id: 1,
},
{
dateApproved: new Date(),
id: 2,
}
];
approved1.every(a => a.dateApproved != null) ? 'APPROVED' : 'PENDING'
// 'APPROVED'
approved2.reduce(every) ? 'APPROVED' : 'PENDING'
// 'PENDING'
I struggle with where I store the currently passed values. Where do I store the "passed" values like Array.every()
does?
function every(previousValue, currentValue, currentIdx, arr) {
if(previousValue.dateApproved !== null && currentValue.dateApproved !== null) {
return currentValue;
}
}
You can use reduce
instead of every
and make it work, but I'd suggest you to use them in there apt places. Both have distinctive requirement.
array.reduce
array.reduce(callback(curentElement, nextElement, index, array),[initialCurrentElement])
Array.reduce has 4 args.
- currentElement: By default, this will be 1st element in array for 1st iteration and then, this variable will hold value that you return. If an initial value is passed, then it will hold that and start from there.
- nextElement: By default it holds second or next element. If initial value is passed, this will hold first value.
- index: this holds the index of current element.
- array: This is the parent array on which we are looping.
- initialCurrentElement: This is an optional argument. If this is passed, looping starts with this.
Following is a sample showing an illustration:
Note:
Array.every
will break on firstfalsey
condition. Array.reduce will not.Array.reduce
is meant to compare 2 values of same array where asArray.every
is meant to compare each values to an expression. Using.reduce
instead of.every
is just an overkill.
var approved2 = [{
dateApproved: null,
id: 1,
}, {
dateApproved: new Date(),
id: 2,
}];
var everyResult = approved2.every(x => {
console.log(x.dateApproved)
x.dateApproved !== null
})
console.log(everyResult)
var reduceResult = approved2.reduce((p, c) => {
console.log(c.dateApproved)
return !p ? p : c.dateApproved !== null
}, true)
console.log(reduceResult? 'Approved': 'Rejected')
这篇关于如何在JavaScript中自定义Array.reduce()中重写Array.every()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!