[英]Javascript Nested for loop with ES6
I guys, I have a nested for loop but I want to do it with an Array map/ES6 way but how does that work with nested forloops? 伙计们,我有一个嵌套的for循环,但是我想用Array map / ES6的方式来做,但是如何与嵌套的forloops一起工作呢?
for (var i = 0; i < enemy.ships.length; i++) {
for (var n = 0; n < enemy.ships[i].location.length; n++) {
if (obj.coordination == enemy.ships[i].location[n]) hit = true;
}
};
I know how to do it when it as not a forloop 我知道当不是forloop时该怎么做
players.map(function(player){if(player.id != socket.id) return enemy = player});
But I can't seem to understand how it should be with Array Maps or something else. 但是我似乎无法理解数组映射或其他东西应该如何。
I need to match the location of the ships location & obj.coordination. 我需要匹配船只位置和obj.coordination的位置。 This is the enemy.ships variable I need to check
这是我需要检查的敌人。
[ { type: 'Aircaft',
size: 5,
rekt: false,
available: 1,
location: [] },
{ type: 'Battleship',
size: 4,
rekt: false,
available: 1,
location: [ 77, 76, 75, 74 ] },
{ type: 'Destroyer',
size: 3,
rekt: false,
available: 2,
location: [ 54, 44, 34 ] },
{ type: 'Submarine',
size: 3,
rekt: false,
available: 3,
location: [] },
{ type: 'Patrolboat',
size: 2,
rekt: false,
available: 4,
location: [] } ]
If all you are looking for is whether any location on any ship matches (and not returning the ship that was hit, et cetera), you can use something like: 如果您要查找的是任何船只上的任何位置是否匹配(并且不返回被击中的船只等),则可以使用以下方法:
const hit = enemy.ships
.map(ship => ship.location)
.some(coordinates => coordinates.some(coordinate => coordinate === obj.coordination ));
If you wanted to return the ship that was hit (or the ships, if multiple ships were allowed to share the same coordinates): 如果要返回被击中的飞船(或飞船,如果允许多艘飞船共享同一坐标):
const hitShips = enemy.ships
.filter(ship => ship.location.some( coordinate => coordinate === obj.coordination ));
Your example of map
is also a little... off. 您的
map
示例也有点...关闭。
The goal of map
isn't to cause side-effects (in fact, it's specifically meant to avoid all side-effects). map
目的不是引起副作用(实际上,它专门用于避免所有副作用)。
The goal of map
is to take one array of objects and return a brand new array of the exact same length, where you have filled the new array based on the objects of the old array. map
的目标是获取一个对象数组并返回完全相同长度的全新数组,在该数组中,您已基于旧数组的对象填充了新数组。
[1, 2, 3].map( x => x + 1 ); // [2, 3, 4]
["a", "b", "c"].map( x => x.toUpperCase() ); // ["A", "B", "C"]
If you just want to check each item and cause side-effects (change a value that exists outside of the function passed in) then use forEach
. 如果只想检查每个项目并引起副作用(更改传入的函数之外的值),请使用
forEach
。
You could use the forEach
method to do it in a more functional way. 您可以使用
forEach
方法以更实用的方式进行操作。 However you won't be able to break from it once the location matches. 但是,一旦位置匹配,您将无法脱离它。 Additional ref: how to stop Javascript forEach?
其他参考: 如何停止Javascript forEach?
enemy.ships.forEach((ship) => {
ship.location.forEach((location) => {
if (obj.coordination === location)
hit = true;
})
})
You could use Array.prototype.find and filter: 您可以使用Array.prototype.find和filter:
For example: 例如:
var compareLocation = (a, b) => a.length === b.length && a.filter((v, i) => v === b[i]).length === a.length;
var enemyShipFound = enemy.ships.find(ship => compareLocation(obj.coordination, ship.location));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.