0

Can I add loop in this code to make it more short or anything else ?

const ARROW_FUNC = (name,age,gender,address) => ({name,age,gender,address});

const PROFILE_1 = ARROW_FUNC('Amit Rastogi',26,'Male','Delhi');
const PROFILE_2 = ARROW_FUNC('Sorya Morya',24,'Male','Rohtak');
const PROFILE_3 = ARROW_FUNC('Ramya Sharma',24,'Female','Delhi');
const PROFILE_4 = ARROW_FUNC('Neeraj Verma',23,'Male','Noida');
const PROFILE_5 = ARROW_FUNC('Himesh Gupta',25,'Male','Delhi');
const PROFILE_6 = ARROW_FUNC('Himani Rathore',31,'Female','Mumbai');
const PROFILE_7 = ARROW_FUNC('Prakash Sharma',20,'Male','Jaipur');
const PROFILE_8 = ARROW_FUNC('Anuradha Basu',29,'Female','Meerut');
const PROFILE_9 = ARROW_FUNC('Sagar Sinha',28,'Male','Haryana');
0

4 Answers 4

3

You could create a 2D array of values. map the array and call the ARROW_FUNC on each inner array by spreading the their values

const ARROW_FUNC = (name, age, gender, address) => ({ name, age, gender, address })

const array = [
  ['Amit Rastogi', 26, 'Male', 'Delhi'],
  ['Sorya Morya', 24, 'Male', 'Rohtak'],
  ['Ramya Sharma', 24, 'Female', 'Delhi']
]

const output = array.map(props => ARROW_FUNC(...props))

console.log(output)

Sign up to request clarification or add additional context in comments.

2 Comments

How can I access individual profile by your code and add new profile ?
@Sagar it's an array. Just access the index you want: const PROFILE_1 = output[0]. Or use destructuring: const [PROFILE_1, PROFILE_2] = output;
0

Data and code should be put separately. It makes the code more maintainable. As described by the above comment. Moreover, it is more desirable to put the data in a different file from the code.

Comments

0

You can use ARROW_FUNC as lambda for a reducer to create an object with all profiles from an array of data:

const data = [
 ['Amit Rastogi',26,'Male','Delhi'],
 ['Sorya Morya',24,'Male','Rohtak'],
 ['Ramya Sharma',24,'Female','Delhi'],
 ['Neeraj Verma',23,'Male','Noida'],
 ['Himesh Gupta',25,'Male','Delhi'],
 ['Himani Rathore',31,'Female','Mumbai'],
 ['Prakash Sharma',20,'Male','Jaipur'],
 ['Anuradha Basu',29,'Female','Meerut'],
 ['Sagar Sinha',28,'Male','Haryana'],
].reduce( (acc, [name, age, gender, place], i) => 
  ({...acc, [`PROFILE_${i + 1}`]: {name, age, gender, place}}), {});

console.log(data.PROFILE_4);

2 Comments

Please explain from .reduce method.
0

@adiga is correct. Here is an alternate approach using Object Model which is scalable in case you want to add more profiles later on. This will avoid having to always keep adding the old data when you want to update the list of profiles. You can try the below code in CodePen

//creating an OBJECT MODEL by the name Profile
class Profile {
  constructor(name, age, gender, address) {
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.address = address;
  }
}

//creating an array to STORE all profiles 
const PROFILELIST = [];

//function to create profiles in BACTH
const createBatchProfiles = (data) => {
  data.map(profile=> {
    const newProfile = new Profile(...profile);
    PROFILELIST.push(newProfile);
  });
};

//function to create SINGLE profile
const createSingleProfile = (name, age, gender, address) => {
  const newProfile = new Profile(name, age, gender, address);
  PROFILELIST.push(newProfile);
};

//Creating new MULTIPLE Profiles in a batch
const newData = [
  ['Amit Rastogi', 26, 'Male', 'Delhi'],
  ['Sorya Morya', 24, 'Male', 'Rohtak'],
  ['Ramya Sharma', 24, 'Female', 'Delhi']
];
createBatchProfiles(newData);

//creating a SINGLE new Profile 
createSingleProfile('Neeraj Verma',23,'Male','Noida');

//Output the list in console
console.log(PROFILELIST);

//Creating even more Profiles
const newerData = [
  ["Himesh Gupta", 25, "Male", "Delhi"],
  ["Himani Rathore", 31, "Female", "Mumbai"],
  ["Prakash Sharma", 20, "Male", "Jaipur"],
  ["Anuradha Basu", 29, "Female", "Meerut"]
]
createBatchProfiles(newerData);
createSingleProfile("Sagar Sinha", 28, "Male", "Haryana");

//console logging updated result
console.log(PROFILELIST);

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.