1

これは私のオブジェクトです:

function Plane (x,z) {  
    this.x = x;
    this.z = z;
}

var plane = new Plane(0,50000);

これらのオブジェクトを含む配列があります:

planesArray.push(plane);

ポイントオブジェクトがあります:

function Point (x,z) {  
    this.x = x;
    this.z = z;
}

var point = new Point(0,-50000);

特定の点を持つオブジェクトが存在するかどうかを確認する必要があるplanesArrayため、点の値 x と y が配列内のいずれかの平面と等しいかどうかを確認し、そうでない場合はアクションを実行します。

私はまだ初心者です。この質問がばかげているように聞こえる場合は、申し訳ありません。

4

2 に答える 2

2

Point配列をループして、それらの属性を持つオブジェクトが見つかったかどうかを示すブール値を返します。この例では、.someメソッドを使用してこの操作を実行します。

var found = planesArray.some(function(plane) {
    return plane.x === x && plane.y === y;
});

if (found) {

}

更新:これは関数と同じコードです。

function found(list, x, y) {
    return list.some(function(plane) {
        return plane.x === x && plane.y === y;
    });
}
于 2013-02-09T16:48:47.017 に答える
0
var point = new Point(0,-50000);
for(var i = 0; i < planesArray.length; i++) {
    var plane = planesArray[i];
    if(plane.x != point.x || plane.z != point.z) {
        //perform action
        //break; //optional, depending on whether you want to perform the action for all planes, or just one
    }
}
于 2013-02-09T16:48:19.143 に答える