我愿意在数组MessageContent.orderedDishes[i]
中运行for循环来获取该数组中的值,但问题是MessageContent.orderedDishes[i]
位于名为textToSend
的变量中我可以& #39; t将其移到变量之外,因为该变量的内容将被发送到另一个视图
shareWithClient(MessageContent){
let textToSend =
'####################' + '\n\n' +
'Dish : ' + MessageContent.orderedDishes[i].dishName + '\n' +
'Category : ' +MessageContent.orderedDishes[i].categoryName+'\n' +
'Qty : ' + MessageContent.orderedDishes[i].qty +'\n\n'+
'####################';
SocialSharing.shareViaWhatsApp(textToSend).then(() => {
// Success!
}).catch(() => {
// Error!
});
}
工作但只打印数组的第一个元素,知道数组有3个元素:
var textToSend = '';
for(var i=0; i<MessageContent.orderedDishes.length; i++){
textToSend = '' +
'####################' + '\n\n' +
'Dish : ' + MessageContent.orderedDishes[i].dishName + '\n' +
'Category : ' +MessageContent.orderedDishes[i].categoryName+'\n' +
'Qty : ' +MessageContent.orderedDishes[i].qty +'\n\n'+
'####################';
}
console.log('textToSend');
答案 0 :(得分:0)
shareWithClient(MessageContent) {
let text = '';
if (MessageContent && MessageContent.orderedDishes) {
MessageContent.orderedDishes.forEach(buildText);
}
shareText();
function buildText(dish) {
text += `####################
Dish : ${ dish.dishName }
Category : ${ dish.categoryName }
Qty : ${ dish.qty }
####################`;
}
function shareText() {
SocialSharing.shareViaWhatsApp(text).then(() => {
// Success!
}).catch(() => {
// Error!
});
}
}