简体   繁体   English

Javascript-如何将一个数组中的点添加到另一个数组中-所以我可以将它们相加并比较两个数组

[英]Javascript - how to add points from one array to another - so I can tally them up and compare the two

I am trying to convert the value of the 2 cards in each Hand array to points, so that I can tally them up and compare the two. 我试图将每个手形阵列中2张卡的值转换为点,以便我可以对它们进行计数并比较两者。

Currently, it says my playerPoints is NaN, so my issue is in the for-of loop or my playerHand.point. 当前,它说我的playerPoints是NaN,所以我的问题是在for-of循环或我的playerHand.point中。

for (let i = 0; i < 2; i++) {
 playerHand.push(dealRandomCard());
 dealerHand.push(dealRandomCard());
}
 // console.log(playerHand);
 // console.log(dealerHand);

let playerPoints = 0,
 dealerPoints = 0;

for (point of playerHand) {
 playerPoints += playerHand.point;
}
console.log(playerPoints);

REVISED - WORKING CODE - Thanks to Nina 修订版-工作代码-感谢Nina

for (let { points } of playerHand {
    playerPoints += points
}

Assuming the objects of a last question of OP of the same kind, it should be points in stead of point . 假设OP的最后一个问题的对象是同类的,它应该是points而不是point

Beside this, you need to take the points property by using a destructuring assignment 除此之外,您还需要使用解构分配来获取points属性

for (let { points } of playerHand) {
    playerPoints += points;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中将数组中的值添加到另一个值中? - how can i add a value from an array to a value from an another one in javascript? 从ajax json请求中,如何将对象动态添加到数组中,以便我可以遍历它们? - From an ajax json request, how can dynamically add the objects to an array so that I can loop through them? 如何从event.target获取DOM属性,以便可以对其进行比较? - How to get the DOM properties from event.target, so I can compare them? 如何在 javascript 中添加购物车中的数字并正确显示小数点后的数字 - How can I add numbers as in a shopping cart in javascript and display them properly with the numbers after the decimal points 我如何比较三个对象数组以便添加或删除新值? - How can i compare three array of objects so i an add or remove new values? 如何每秒增加一个数字,以便以后显示或使用? - How to add a number every one second so I can show or use them later? 如何添加两个或多个数组元素(数字) - How can I add up two or more array elements (numbers) 如何从“表格”中提取2个数字并将其相加,以便输出结果 - How can I take 2 numbers from “form” and add them so I can output the result 我有两个数组。 我想以一种可以从另一个数组中获取最接近的值的方式比较它们 - I have two array. I want to compare both of them in a way that I can get the nearest value from the other array 如何在Javascript中将一个数组添加到另一个数组中? - How can I add an array into another array in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM