-1

I have created a constructor function

  constructor(People) {
    this.PeopleName = People['Name'];
    this.PeopleId = People['Id'];
    this.PeopleGender = People['Gender'];
    this.Peopleunit = People['unit'];

const Name = ["Ashok", "Payal"];
const Id = ["1", "2"];
const Gender = ["M", "F"];

Now I have to map all the values that will return new People values as object.How can i pass all these values when we intialize an Object . Output will be somthing like this.

0:{Name:'Ashok', Id: '1', Gender: 'M', unit: '234'}
1:{Name:'Payal', Id: '2', Gender: 'f', unit: '234'}
    

Here Unit will be static.

I have tried for loop to do this but not works for me.Can anyone help me the acheive this objective

0

1 Answer 1

0

You can loop through and create object like this:

const Name = ["Ashok", "Payal"];
const Id = ["1", "2"];
const Gender = ["M", "F"];
const allPerson = [];

Name.forEach((person, i) => {
    // You can instantiate your People object here
    allPerson.push({"Name": person, "Id": Id[i], "Gender": Gender[i], "unit": '234'})
});

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

3 Comments

As Object is already predefined so it wodun't work
@AnviCreations I am not sure what you are referring here. If you run this code and see console log. I believe that's what your expected output was. Right?
Flag as duplicate, this is identical to the dupe target noted above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.