简体   繁体   中英

How do I make my JavaScript code more short?

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');

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)

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.

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);

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM