通过比较两个具有相同编号的数组,IndexOf返回false。
它也不适用于单个数字,例如position [i] .location [j]。 我想比较整个数组,而不仅仅是单个整数。 [32,33,34]和[35,34,33]也应该返回true ...还有更好的方法吗?
(...)
//Array of ship locations
ships: [
{location:[13, 23, 33]},
{location:[2, 12, 22]},
{location:[15, 14, 16]},
{location:[17, 18, 19]},
{location:[31, 41, 51]},
{location:[40, 41, 42]}
],
//Array with additional ship location
position: [
{location: [2, 12, 22]},
{location: [40, 41, 42]}
],
collision: function(position) {
if(this.ships.length > 1) {
for(var i = 0; i < this.ships.length; i++) {
for(var j = 0; j < position.length; j++) {
if(this.ships[i].location.indexOf(this.position[j].location) >= 0) {
return true;
} else {
return false;
}
}
}
} else {
return false;
}
}
console.log(model.collision(model.position)); //returns false
答案 0 :(得分:1)
.indexOf方法将不适用于复杂对象,例如javascript中的数组或对象文字,因为这种行为称为“传递值与引用传递”。与原始值(字符串,数字,布尔值)的存储方式以及复杂值(对象)的存储方式有关,这是一个冗长的主题。
如果要比较两个数组是否“相同”(即,如果两个数组包含相同的整数序列),则必须检查每个数组中的每个索引。
答案 1 :(得分:1)
要在您的场景中查找冲突,您需要对第一组进行迭代,并针对每个数组检查另一组数组中是否存在任何/某些元素。这是执行此操作的代码:
let ships = [ {location:[13, 23, 33]}, {location:[2, 12, 22]}, {location:[15, 14, 16]}, {location:[17, 18, 19]}, {location:[31, 41, 51]}, {location:[40, 41, 42]} ]
let position = [{location: [22, 12, 22]},{location: [40, 41, 42]}]
// Compares if two arrays of numbers in this case are equal by making sure values
// at array indexes are equal
let isSameArray = (a,b) => a.every((number, index) => b[index] === number)
// Compares the sets by iterating over each of the elements inside and
// calling `isSameArray`
let contains = (a,b) => a.some(x => b.some(y => isSameArray(x.location, y.location)))
console.log(contains(ships, position)) // true
console.log(contains([{location:[22, 12, 22]}],[{location:[2, 12, 22]}])) // false
ES5
中的this看起来像这样:
let ships = [ {location:[13, 23, 33]}, {location:[2, 12, 22]}, {location:[15, 14, 16]}, {location:[17, 18, 19]}, {location:[31, 41, 51]}, {location:[40, 41, 42]} ]
let position = [{location: [22, 12, 22]},{location: [40, 41, 42]}]
let isSameArray = function(arr1, arr2){
return arr1.every(function(number, index){
return arr2[index] === number
})
}
let contains = function(setA, setB){
return setA.some(function(objA) {
return setB.some(function(objB) {
return isSameArray(objA.location, objB.location)
})
})
}
console.log(contains(ships, position)) // true
console.log(contains([{location:[22, 12, 22]}],[{location:[2, 12, 22]}])) // false
现在此代码使用Array.some和Array.every,它们在第一种情况下均返回布尔值,指示all
元素与谓词函数匹配,在第二种情况中至少存在一个匹配项。
只要您包含用于比较location
数字数组的实际函数,也可以使用您的方法来实现:
let ships = [ {location:[13, 23, 33]}, {location:[2, 12, 22]}, {location:[15, 14, 16]}, {location:[17, 18, 19]}, {location:[31, 41, 51]}, {location:[40, 41, 42]} ]
let position = [{location: [22, 12, 22]},{location: [40, 41, 42]}]
let isSameArray = function(arr1, arr2){
return arr1.every(function(number, index){
return arr2[index] === number
})
}
let collision = function(arr1, arr2) {
for(var i = 0; i < arr1.length; i++) {
for(var j = 0; j < arr2.length; j++) {
if(isSameArray(arr1[i].location, arr2[j].location))
return true
}
}
return false
}
console.log(collision(ships, position)) // true
答案 2 :(得分:0)
我尝试了.includes();而不是.indexOf();而且效果很好。甚至我都不知道为什么...