![](/img/trans.png)
[英]Check if object from one array is included inside another array of objects
[英]Arrays Inside Objects: Conditional Updating Of One Object's Array From Another Object's Array
免責聲明
我完全不知道如何在不深入上下文的情況下簡潔地描述我試圖解決的問題的性質。 我花了很長時間才想出一個合適的標題。 出於這個原因,我發現幾乎不可能在這里和整個網絡上找到可以幫助我的答案。 有可能我的問題可以歸結為簡單的問題,這里已經有了答案。 如果是這種情況,我為精心制作的副本道歉
TL; 博士
我有兩個數組:一個主數組members
和一個目標數組neighbours
(技術上有很多目標數組,但這是 tl;dr)。 主數組是我的自定義組對象的一個屬性,它自動填充了自定義球對象。 目標數組是我的自定義球對象的一個屬性。 我需要掃描members
數組中的每個元素並計算該元素與members
組中的每個其他元素之間的距離。 如果在當前元素的設定距離內存在其他元素,則需要將這些其他元素復制到當前元素的目標數組中。 這種檢測需要實時進行。 當兩個元素變得足夠接近成為鄰居時,它們將被添加到各自的neighbours
數組中。 當它們變得太遠而不能被視為鄰居時,它們需要從各自的neighbours
數組中刪除。
語境
我的問題主要是關於數組迭代、比較和操作,但要了解我的確切困境,我需要提供一些上下文。 我的上下文代碼片段已盡可能簡短。 我正在為我的項目使用 Phaser 庫,但我的問題與 Phaser 無關。
我制作了自己的對象,稱為 Ball。 目標代碼是:
Ball = function Ball(x, y, r, id) {
this.position = new Vector(x, y); //pseudocode Phaser replacement
this.size = r;
this.id = id;
this.PERCEPTION = 100;
this.neighbours = []; //the destination array this question is about
}
我所有的 Ball 對象(到目前為止)都位於一個組中。 我創建了一個BallGroup
對象來放置它們。相關的BallGroup
代碼是:
BallGroup = function BallGroup(n) { //create n amount of Balls
this.members = []; //the main array I need to iterate over
/*fill the array with n amount of balls upon group creation*/
for (i = 0; i < n; i++) {
/*code for x, y, r, id generation not included for brevity*/
this.members.push(new Ball(_x, _y, _r, _i)
}
}
我可以使用以下內容創建一組 4 個 Ball 對象:
group = new BallGroup(4);
這很好用,使用我沒有包含的 Phaser 代碼,我可以單擊/拖動/移動每個球。 我還有一些Phaser.utils.debug.text(...)
代碼,它在一個易於閱讀的 4x4 表格中顯示每個球之間的距離(當然有重復,因為距離 Ball0->Ball3 與距離 Ball3-> 相同)球0)。 對於文本覆蓋,我使用嵌套的for
循環計算距離:
for (a = 0; a < group.members.length; a++) {
for (b = 0; b < group.members.length; b++) {
distance = Math.floor(Math.sqrt(Math.pow(Math.abs(group.members[a].x - group.members[b].x), 2) + Math.pow(Math.abs(group.members[a].y - group.members[b].y), 2)));
//Phaser text code
}
}
現在是我問題的核心。 每個 Ball 都有一個檢測范圍PERCEPTION = 100
。 我需要遍歷每個group.members
元素並計算該元素( group.members[a]
)與group.members
數組中的每個其他元素之間的group.members
(我可以做這個計算)。 我的問題是我不能然后復制其距離那些要素group.members[a]
是< PERCEPTION
到group.members[a].neighbours
陣列。
我將主數組 ( BallGroup.members
) 放在一個對象中,將目標數組放在不同對象 ( Ball.neighbours
) 中的原因是,我需要 BallGroup 中的每個 Ball 都知道它自己的鄰居,而不必關心鄰居的情況用於 BallGroup 中的所有其他 Ball。 但是,我相信這兩個數組(主數組和目標數組)位於不同對象中的事實是我遇到這么多困難的原因。
但有一個問題。 這種檢測需要實時進行,當兩個球不再在PERCEPTION
范圍內時,它們必須從它們各自的neighbours
陣列中移除。
例子
group.members[0] -> no neighbours
group.members[1] -> in range of [2] and [3]
group.members[2] -> in range of [1] only
group.members[3] -> in range of [1] only
//I would then expect group.members[1].neighbours to be an array with two entries,
//and both group.members[2].neighbours and group.members[3].neighbours to each
//have the one entry. group.members[0].neighbours would be empty
我自己把 group.members[2] 和 group.members[3] 拖到一個角落
group.members[0] -> no neighbours
group.members[1] -> no neighbours
group.members[2] -> in range of [3] only
group.members[3] -> in range of [2] only
//I would then expect group.members[2].neighbours and group.members[3].neighbours
//to be arrays with one entry. group.members[1] would change to have zero entries
我的嘗試
我已經嘗試了足夠多的事情來迷惑任何人,這就是我來這里尋求幫助的原因。 我首先嘗試了復雜的嵌套for
循環和if/else
語句。 這導致鄰居被無限添加並開始變得太復雜而我無法跟蹤。
我查看了Array.forEach
和Array.filter
。 我不知道forEach
是否可以用於我需要的東西,我很興奮地了解filter
作用(返回與條件匹配的元素數組)。 使用Array.filter
它要么為 Ball 對象提供零個鄰居,要么將所有其他 Ball 作為鄰居而不考慮距離(我不知道它為什么這樣做,但這絕對不是我需要它做的) )。 在撰寫此問題時,我當前用於檢測鄰居的代碼是:
BallGroup = function BallGroup(n) {
this.members = []; //the main array I need to iterate over
//other BallGroup code here
this.step = function step() { //this function will run once per frame
for (a = 0; a < this.members.length; a++) { //members[a] to be current element
for (b = 0; b < this.members.length; b++) { //members[b] to be all other elements
if (a != b) { //make sure the same element isn't being compared against itself
var distance = Math.sqrt(Math.pow(Math.abs(this.members[a].x - this.members[b].x), 2) + Math.pow(Math.abs(this.members[a].y - this.members[b].y), 2));
function getNeighbour(element, index, array) {
if (distance < element.PERCEPTION) {
return true;
}
}
this.members[a].neighbours = this.members.filter(getNeighbour);
}
}
}
}
}
我希望我的問題是有道理的,並且解釋得足夠好。 我確切地知道在我自己的項目的上下文中我需要做什么,但是將其用語言表達出來讓其他人了解不了解我的項目的人一直是一個挑戰。 我一直在學習 Javascript,到目前為止一直做得很好,但這種特殊情況讓我完全迷失了方向。 我陷得太深了,但我不想放棄——我要學習!
非常感謝那些花時間閱讀我很長的帖子並試圖提供一些見解的人。
編輯:將 > 更改為 <
我正在學習更多關於對象文字的知識,我正在嘗試學習 JS 以擺脫對 jQuery 的依賴。 我正在制作一個簡單的庫,並制作了一個將一個對象的屬性添加到另一個對象的函數。 它未經測試,但我認為如果您應用類似的東西可能會有所幫助。 我會努力尋找我的資源。 順便說一句,我現在手頭沒有這些文章,但我記得使用 new 可能會導致並發症,抱歉我不能再進一步了,我會在找到時發布更多信息。
/* augment(Obj1, Obj2) | Adds properties of Obj2 to Obj1. */ // xObject has augment() as a method called aug var xObject = { aug: augument } /* Immediately-Invoked Function Expression (IIFE) */ (function() { var Obj1 = {}, Obj2 = { bool: true, num: 3, str: "text" } xObject.aug(Obj1, Obj2); }()); // invoke immediately function augment(Obj1, Obj2) { var prop; for (prop in Obj2) { if (Obj2.hasOwnProperty(prop) && !Obj1[prop]) { Obj1[prop] = Obj2[prop]; } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.