3 回答

TA貢獻1813條經驗 獲得超2個贊
如果您只想映射一個數組,那么您可能需要這樣的東西:
const [born, weight, height] = [
process_date(arry[0]),
process_weight(arry[1]),
process_height(array[2])
]
如果有多個數組,則需要自己處理,您可以創建一個函數,該函數接受輸入數組并返回映射數組:
function mapArray(arr) {
return [
process_date(arr[0]),
process_weight(arr[1]),
process_height(arr[2])
]
}
arry.forEach(arr => {
const [born, weight, height] = mapArray(arr);
// do stuff with the variables here...
})

TA貢獻1853條經驗 獲得超6個贊
您可以將函數放入對象中。然后將你的值放入一個對象數組中,這樣你就可以擁有元數據來告訴值它應該調用什么函數。
例子
const valueObjects = [{
type: "date",
value: "22-03-1995"
}, {
type: "weight",
value: 80.5
}]
const calculations = {
date: function process_date(d) {...},
weight: function process_weight(w) {...}
};
valueObjects.forEach(valueObject => {
const processType = calculations[valueObject.type];
processType(valueObject.value);
})

TA貢獻1824條經驗 獲得超6個贊
希望這可以幫到你
arry = ["22-03-1995", 80.5, 1.83];
arrayFunc = [function process_date(d) { ... }, function process_weight(w) { ... }, function process_height(h) { ... } ]
array.forEach(myFunction);
let results = []
function myFunction(item, index) {
results << arrayFunc[index](item)
}
let born, weight, height;
[born, weight, height] = results;
console.log(born);
console.log(weight);
console.log(height);
添加回答
舉報