3

Let's say that I have a list of 5 people's name

let listNames = [Sam, Ash, Moe, Billy, Kenzi]

and I want for each of the names to have properties of doneHomeWork and lazy using class

class Person {
    constructor() {
        this.doneHomeWork = 0
        this.lazy = false
    }
}

instead of assigning each names as so :

const Sam = new Person()
const Ash = new Person()
const Moe = new Person()
const Billy = new Person()
const Kenzi = new Person()

I was thinking doing this

listNames.forEach(name => {
    name = new Person()
})

However, in my ESLint it's giving me an error

Assignment to function parameter 'name' no-param-reassign

This seems trivial, but for some reason I am having trouble re-factoring this.

4 Answers 4

2

The issue is that the name variable is the one used for cycling inside the loop. You're changing this value in the first iteration of the loop. This is the reason of your error Assignment to function parameter 'name' no-param-reassign.

Then you are trying to use dynamic names as variables names. If you want to do it, the way is using bracket notation, so you could do it in this way:

class Person {
    constructor() {
        this.doneHomeWork = 0
        this.lazy = false
    }
}
let persons = [];
let listNames = ['Sam', 'Ash', 'Moe', 'Billy', 'Kenzi'];

correctListNames.forEach(name => {
    persons[name] = new Person()
})

console.log(persons);
Sign up to request clarification or add additional context in comments.

2 Comments

awesome! I just modified let persons = {} so I can avoid inserting an array, and correctListNames should be listNames. Other than that, thanks!
Sure objects can be accessed with the same notation. You are welcome :)
2

You can use ES6 array destructuring and assign new instance of Person class to each name.

class Person {
  constructor() {
    this.doneHomeWork = 0;
    this.lazy = false;
  }
}


const [Sam, Ash, Moe, Billy, Kenzi] = Array.from(Array(5), e => new Person)

console.log(Sam)
console.log(Ash)

1 Comment

Yeah but in this way you assume a finite number of persons. Otherwise this solution can't be dynamic and I t can't be applied. I think he was wondering how to iterate over an array of persons and create corresponding objects
1

Instead of trying to dynamically generate this

const Sam = new Person();
const Randy = new Person():

You can get something similar by like this,

const persons = {
  Sam: new Person(),
  Randy: new Person(),
};

Then you can access them using persons['Sam'] or persons.Sam. Now to achieve this from an array,

const arr = ['Sam', 'Randy'];
const persons = {};
arr.forEach(name => {
  persons[name] = new Person();
});

As for the eslint warning that you are getting is because you are trying to change the value of a parameter of a function. You can turn if off by setting the rule to 0 in your eslint configuration file. However I would recommend not to turn if off.

Comments

0

You can use ES6 destructuring feature to achieve this:

class Person {
    constructor() {
        this.doneHomeWork = 0
        this.lazy = false
    }
}
let arr = [];
for(let i=0;i<6;i++) {
	arr.push(new Person());
}


const [Sam, Ash, Moe, Billy, Kenzi] = [...arr];

console.log(Sam);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.