2017-08-31 88 views
0

我希望最終的結果應該有價值。這個減速器有什麼問題

「錯誤:無效UpdatableBy類型有heLlo1」

相反,它甚至沒有打印任何控制檯消息或進入減少功能。我可以使用foreach做到這一點,但想到使用reduce方法來嘗試這個。

在t時的最終輸出是 「heLl01」

pobject = { 
 
    UpdatableBy: ["heLlo1"] 
 
} 
 
let t; 
 
let updatedUpdatableBy = [] 
 
t = pobject.UpdatableBy.reduce((allerrors, val) => { 
 
    console.log(allerrors) 
 
    console.log(val) 
 
    if (typeof val !== "string") { 
 
    allerrors += "Error: Invalid UpdatableBy data type got " + typeof val 
 
    } else { 
 
    if (val.toUpperCase() == "HELLO") { 
 
     updatedUpdatableBy.push("Hello") 
 
    } else if (val.toUpperCase() == "HI") { 
 
     updatedUpdatableBy.push("Hi") 
 
     console.log("Hi") 
 
    } else if (val.toUpperCase() == "HOW") { 
 
     updatedUpdatableBy.push("How") 
 
     console.log("How") 
 
    } else { 
 
     allerrors += "Error: Invalid UpdatableBy type got " + val 
 
     console.log(allerrors) 
 
    } 
 
    } 
 
    return allerrors 
 
}); 
 
console.log(t)

回答

1

參見:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

arr.reduce(callback[, initialValue]) 

initialValue [Optional]

Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce on an empty array without an initial value is an error.

所以你需要通過初值追加到,你的情況是一個空字符串。

pobject = { 
 
    UpdatableBy: ["heLlo1"] 
 
} 
 
let t; 
 
let updatedUpdatableBy = [] 
 
t = pobject.UpdatableBy.reduce((allerrors, val) => { 
 
    if (typeof val !== "string") { 
 
    allerrors += "Error: Invalid UpdatableBy data type got " + typeof val 
 
    } else { 
 
    if (val.toUpperCase() == "HELLO") { 
 
     updatedUpdatableBy.push("Hello") 
 
    } else if (val.toUpperCase() == "HI") { 
 
     updatedUpdatableBy.push("Hi") 
 
     console.log("Hi") 
 
    } else if (val.toUpperCase() == "HOW") { 
 
     updatedUpdatableBy.push("How") 
 
     console.log("How") 
 
    } else { 
 
     allerrors += "Error: Invalid UpdatableBy type got " + val 
 
    } 
 
    } 
 
    return allerrors 
 
}, ""); 
 
console.log(t)

0

減速機的蓄電池需要有一個值來啓動追加到:
[].reduce((acc, next) => {}, defaultAcc)
在這種情況下,這將是""

pobject = { 
 
    UpdatableBy: ["heLlo1"] 
 
} 
 
let t; 
 
let updatedUpdatableBy = [] 
 
t = pobject.UpdatableBy.reduce((allerrors, val) => { 
 
    if (typeof val !== "string") { 
 
    allerrors += "Error: Invalid UpdatableBy data type got " + typeof val 
 
    } else { 
 
    if (val.toUpperCase() == "HELLO") { 
 
     updatedUpdatableBy.push("Hello") 
 
    } else if (val.toUpperCase() == "HI") { 
 
     updatedUpdatableBy.push("Hi") 
 
     console.log("Hi") 
 
    } else if (val.toUpperCase() == "HOW") { 
 
     updatedUpdatableBy.push("How") 
 
     console.log("How") 
 
    } else { 
 
     allerrors += "Error: Invalid UpdatableBy type got " + val 
 
    } 
 
    } 
 
    return allerrors 
 
    // here 
 
}, ""); 
 
console.log(t)

0

如果數組只有1元,而且也沒有initialValue參數reduce(),它不打擾調用該函數,它只是返回一個值。從MDN

If the array has only one element (regardless of position) and no initialValue is provided, or if initialValue is provided but the array is empty, the solo value will be returned without callingcallback .

這是因爲該功能應該數組元素與先前的迭代中積累的結果結合起來。如果只有一個元素,那麼就沒有累積的結果來結合它。 initialValue是在第一次迭代時默認使用的,但如果沒有提供,它根本不能調用該函數。

由於您通過將結果串聯在一個字符串中來累計結果,因此可以使用""作爲initialValue參數。