Reputation: 1109
probably a simple one here but just wondering what the best way to get this working is. I'm currently getting the console.log for purchaseUnitArray as undefined and I know something doesn't look quite right. I simply want to return an array of purchaseUnits, with the individual charity non_profit id and split amount in each. Any help would be great. Thanks!
const charities = [948, 182, 584];
const amount = 1000;
const splitPurchaseUnits = (nonprofit_id, amount) => {
const purchaseUnit = {
custom_id: `${nonprofit_id}`,
amount: {
value: amount,
currency_code: "USD",
breakdown: {
item_total: {
currency_code: "USD",
value: amount,
},
},
},
};
};
const purchaseUnitArray =
charities &&
charities.map((charity) => {
return splitPurchaseUnits(charity, amount / charities.length);
});
console.log(purchaseUnitArray, "array");
Upvotes: 1
Views: 61
Reputation: 22265
I will do that this way:
const charities = [948, 182, 584];
const amount = 1000;
const splitPurchaseUnits=(charities,amount)=>charities.map(nonprofit_id=>
({ custom_id: `${nonprofit_id}`
, amount:
{ value: amount
, currency_code: 'USD'
, breakdown:
{ item_total:
{ currency_code: 'USD'
, value: amount
} } } }))
console.log( splitPurchaseUnits(charities, amount) )
.as-console-wrapper {max-height: 100%!important;top:0;}
Upvotes: 1
Reputation: 206008
Try to implicit return like:
const charities = [948, 182, 584];
const amount = 1000;
const splitPurchaseUnits = (id) => ({
custom_id: id,
amount: {
value: amount / charities.length,
currency_code: 'USD',
breakdown: {
item_total: {
currency_code: 'USD',
value: amount,
}
}
}
});
const purchaseUnitArray = charities.map(splitPurchaseUnits);
console.log(purchaseUnitArray)
Upvotes: 1
Reputation: 25398
You need to return some value from the function splitPurchaseUnits
else return undefined
internally by JS.
const charities = [948, 182, 584];
const amount = 1000;
const splitPurchaseUnits = (nonprofit_id, amount) => {
const purchaseUnit = {
custom_id: `${nonprofit_id}`,
amount: {
value: amount,
currency_code: "USD",
breakdown: {
item_total: {
currency_code: "USD",
value: amount,
},
},
},
};
return purchaseUnit;
};
const purchaseUnitArray =
charities &&
charities.map((charity) => {
return splitPurchaseUnits(charity, amount / charities.length);
});
console.log(purchaseUnitArray, "array");
Upvotes: 1
Reputation: 1881
Just looks like you're not returning the purchaseUnit
at the end of your function! You can either do:
return purchaseUnit;
or instead:
return {
custom_id: nonprofit_id,
// etc
}
(or completely remove the return like the other answer shows).
As a heads up, it looks like there's a missing bracket at the end of splitPurchaseUnits
. Not sure if a bug or accident in creating the question.
Upvotes: 1