How to compare two arrays

Hello. I have some objects call Planets. Each planet have a array call Labels, fill with a different information for each object. What i need is a way to find for every obj if that array have some value in common.

For example; Planet01 labels: "investigation", "love" Planet02 labels: "investigation", "hate", return true becouse "investigation"

in the case that different Obj array have the same value, return true and connected with a line. Im a little bit lost and is my first time programming in p5.js. How can compare the arrays of each object? here is my code:

 let planet = [];
var postsIndex;
var labels = [];
var title;
var posts= [];
var r=400;;


function setup() {
    createCanvas(windowWidth, windowHeight);
    background(25);
    loadJSON('https://www.googleapis.com/blogger/v3/blogs/6719264076843274138/posts?key=AIzaSyDljSVan2cU0a0kq0iqF0cglFUzXnWgRP8', gotData);
    smooth();

}

function gotData(data){
    // console.log(data.items.length);
    postsIndex = data.items.length;
    posts = data.items;


    fill(255);
    textSize(25);
    text("posts: " + postsIndex, 100, 100);

    for(var i = 0; i < postsIndex; i++){
     labels = data.items[i].labels;
     title = data.items[i].title;

      var radius = random(r);
    var a = random(TWO_PI);
        var x = width/2+  cos(a)*radius;
        var y = height/2+ sin(a)*radius;

        let p = new Planet(x,y, 10, labels,title, i);
         var overlapping = false;

            for(var j = 0; j < planet.length; j++){
                var other = planet[j];
                var d = dist(p.x, p.y, other.x, other.y);
                if(d < p.r + other.r){
                    overlapping = true;
                    break;
                }
    }
    if(!overlapping){
    planet.push(p);
    }
}

}


function draw() {

background(25);
connected();
// console.log(frameRate());

fill(255);
textSize(15);
text("postCount: " + postsIndex, 100, 100);

push();
translate(width/2, height/2);
noFill();
stroke(255);
ellipse(0,0, r*2, r*2);
pop();

for(let p of planet){
            p.display();
    }

}

function mousePressed(){

    for(var i = 0; i < planet.length; i++){
        planet[i].clicked(mouseX, mouseY);
        planet[i].checking();
    }

}

function connected(){

        for(let p of planet){
                let conectado = false;
                for(let other of planet){
                    if(p!==other && p.isConnected(other)){
                        conectado = true;
                    }
                    if(conectado){
                        line(p.x, p.y, other.x, other.y);
                    }
                }
    }
}




class Planet{

    constructor(x, y, r, labels, title, id){

        this.x = x;
        this.y = y;
        this.r = r;
        this.labels = labels;
        this.title = title;
        this.over = false;
        this.brightness = 255;
        this.id = id;

    }


    display(){

        noStroke();
        strokeWeight(1);
        fill(this.brightness);
        stroke(255);
        ellipseMode(CENTER);
        ellipse(this.x, this.y, this.r*2, this.r*2);
        // console.log(this.over);
    }

    checking(){
        if(this.over){
          console.log(this.id,this.title, this.labels);
            this.brightness = 0;
        }else{
            this.brightness = 255;
        }
    }


        isConnected(other){ //this is the problematic function

for(var i =0; i<labels.length;i++){
    if(this.labels[i] == other.labels){
    return true;
}else{
    return false;
}
return false;
}

}

     clicked(px, py) {
         let d = dist(px, py, this.x, this.y);
            if(d < this.r){
                this.over = true;
            }else{
                this.over = false;
            }
    }

}

Answers

  • Answer ✓

    autoSolved:

    isConnected(other){
    
    for(var i =0; i<this.labels.length;i++){
        if(this.labels.includes(other.labels[i])){
        return true;
    }else{
        return false;
        }
    }
    }
    
  • edited May 11

    pressing ctrl-t in the editor will indent your code nicely.

    nicely indented code is easier to read.

    code that's easier to read is easier to understand.

    code that's easier to understand is easier to debug.

  • sorry koogs. im in atom and i dont know how autoindent.

    isConnected(other){
    
    for(var i =0; i<this.labels.length;i++){
        if(this.labels.includes(other.labels[i])){
        return true;
    }else{
        return false;
        }
    }
    }
    

    this function doesnt work right. It seems that if one object have a label in the array that other object dont have return false. But not always... i dont know.. again, what i need is compare the array of all objects and connected if at least one element match..

  • edited May 12

    I'm on Atom and I don't know how to auto-indent.

    http://JsBeautifier.org

  • Answer ✓
    isConnected({ labels }) {
      for (const label of labels)  if (this.labels.includes(label))  return true;
      return false;
    }
    
Sign In or Register to comment.