Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

adding some clases to array and looping them through this array?

i am trying to push a class to an empty array . then use for to loop through classes in this array but it is not working. is there another way to loop through classes?nothing return in the console , no errors shown

let a=[]
class push_array{
  constructor (first , second) {
    this.first=first;
    this.second=second
    function pushing(){
      a.push(this)
      pushing()
      console.log(a)
      
    }
    
  }
  
}
const result=new push_array('fdsfs','fdsfdsf')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pushing is called from within pushing, so the logic that pushes the instance to the array is never executed.

let a = []
class push_array {
  constructor(first, second) {
    this.first = first;
    this.second = second;
    a.push(this);
  }
}

const result = new push_array('fdsfs', 'fdsfdsf');
console.log(a);

Its unclear what your goal is, but using variable outside of a class could make your code complex very fast. If you’re storing the result, why not just push the object to the array after instantiating?

let a = []

class push_array {
  constructor(first, second) {
    this.first = first;
    this.second = second;
  }
}

const result = new push_array('fdsfs', 'fdsfdsf');
a.push(result);

console.log(a);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading