Javascript 索引不´;不能使用两个对象数组
通过比较两个数字相同的数组,IndexOf返回false 它也不适用于单个数字,例如位置[i]。位置[j]。 我想比较整个数组,而不仅仅是单个整数。 [32,33,34]&[35,34,33]也应该返回true。。。有更好的办法吗Javascript 索引不´;不能使用两个对象数组,javascript,arrays,indexof,Javascript,Arrays,Indexof,通过比较两个数字相同的数组,IndexOf返回false 它也不适用于单个数字,例如位置[i]。位置[j]。 我想比较整个数组,而不仅仅是单个整数。 [32,33,34]&[35,34,33]也应该返回true。。。有更好的办法吗 (...) //Array of ship locations ships: [ {location:[13, 23, 33]}, {location:[2, 12, 22]}, {location:[15, 14, 1
(...)
//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
(…)
//船舶位置数组
船舶:[
{地点:[13,23,33]},
{地点:[2,12,22]},
{地点:[15,14,16]},
{地点:[17、18、19]},
{地点:[31,41,51]},
{地点:[40,41,42]}
],
//具有附加船舶位置的阵列
职位:[
{地点:[2,12,22]},
{地点:[40,41,42]}
],
碰撞:功能(位置){
如果(this.ships.length>1){
for(var i=0;i=0){
返回true;
}否则{
返回false;
}
}
}
}否则{
返回false;
}
}
控制台日志(模型碰撞(模型位置))//返回false
由于被称为“按值传递vs按引用传递”的行为,.indexOf方法无法处理javascript中的数组或对象文本等复杂对象。关于原语值(字符串、数字、布尔值)的存储方式与复杂值(对象)的存储方式,这是一个冗长的主题
如果要比较两个数组是否“相同”(即,如果两个数组包含相同的整数序列),则必须检查每个数组中的每个索引 在javascript中,.indexOf方法无法处理数组或对象文本等复杂对象,因为其行为称为“按值传递vs按引用传递”。关于原语值(字符串、数字、布尔值)的存储方式与复杂值(对象)的存储方式,这是一个冗长的主题
如果要比较两个数组是否“相同”(即,如果两个数组包含相同的整数序列),则必须检查每个数组中的每个索引 我试过了。包括();而不是.indexOf();而且效果很好。甚至我也不知道为什么…我试过了。includes();而不是.indexOf();而且效果很好。即使我也不知道为什么…要在场景中找到冲突,您需要迭代第一个集合,并为每个数组检查其他集合中是否存在任何/部分元素。以下是执行此操作的代码:
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]}]
//通过确保值,比较在这种情况下两个数字数组是否相等
//至少数组索引相等
设isSameArray=(a,b)=>a.every((数字,索引)=>b[index]==number)
//通过迭代内部和外部的每个元素来比较集合
//调用`IsSamerarray`
让contains=(a,b)=>a.some(x=>b.some(y=>issamerarray(x.location,y.location)))
log(包含(装运、位置))//true
log(包含([{location:[22,12,22]}],{location:[2,12,22]}])//false
要在场景中查找冲突,需要迭代第一组数组,并为每个数组检查另一组数组中是否存在任何/某些元素。以下是执行此操作的代码:
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]}]
//通过确保值,比较在这种情况下两个数字数组是否相等
//至少数组索引相等
设isSameArray=(a,b)=>a.every((数字,索引)=>b[index]==number)
//通过迭代内部和外部的每个元素来比较集合
//调用`IsSamerarray`
让contains=(a,b)=>a.some(x=>b.some(y=>issamerarray(x.location,y.location)))
log(包含(装运、位置))//true
log(包含([{location:[22,12,22]}],{location:[2,12,22]}])//false
这将为您的问题提供定义的答案。是的,确实方法includes()工作得非常好!即使在部分相等的数组上…这也将为您的问题提供定义的答案。是的,确实方法includes()工作得非常好!即使在部分相等的数组上。。。