gistlib
index.tsx// Create an object to store user information let userObj = {}; // Add 10 users to the object userObj[1] = { name: 'John', age: 25 }; userObj[2] = { name: 'Emily', age: 32 }; userObj[3] = { name: 'Jane', age: 28 }; userObj[4] = { name: 'Michael', age: 30 }; userObj[5] = { name: 'Sarah', age: 26 }; userObj[6] = { name: 'David', age: 29 }; userObj[7] = { name: 'Amy', age: 33 }; userObj[8] = { name: 'Peter', age: 27 }; userObj[9] = { name: 'Rachel', age: 31 }; userObj[10] = { name: 'Thomas', age: 24 }; // Extract the first 5 users and add them to a new object let userObj2 = {}; for (let i = 1; i <= 5; i++) { userObj2[i] = userObj[i]; } 646 chars22 lines
// Create an object to store user information let userObj = {}; // Add 10 users to the object userObj[1] = { name: 'John', age: 25 }; userObj[2] = { name: 'Emily', age: 32 }; userObj[3] = { name: 'Jane', age: 28 }; userObj[4] = { name: 'Michael', age: 30 }; userObj[5] = { name: 'Sarah', age: 26 }; userObj[6] = { name: 'David', age: 29 }; userObj[7] = { name: 'Amy', age: 33 }; userObj[8] = { name: 'Peter', age: 27 }; userObj[9] = { name: 'Rachel', age: 31 }; userObj[10] = { name: 'Thomas', age: 24 }; // Extract the first 5 users and add them to a new object let userObj2 = {}; for (let i = 1; i <= 5; i++) { userObj2[i] = userObj[i]; }
Alternatively, using array methods:
index.tsx// Create an array of user objects let users = [ { name: 'John', age: 25 }, { name: 'Emily', age: 32 }, { name: 'Jane', age: 28 }, { name: 'Michael', age: 30 }, { name: 'Sarah', age: 26 }, { name: 'David', age: 29 }, { name: 'Amy', age: 33 }, { name: 'Peter', age: 27 }, { name: 'Rachel', age: 31 }, { name: 'Thomas', age: 24 } ]; // Extract the first 5 users and add them to a new array let users2 = users.slice(0, 5); // Convert the array to an object let userObj = {}; users2.forEach((user, i) => { userObj[i+1] = user; }); 551 chars23 lines
// Create an array of user objects let users = [ { name: 'John', age: 25 }, { name: 'Emily', age: 32 }, { name: 'Jane', age: 28 }, { name: 'Michael', age: 30 }, { name: 'Sarah', age: 26 }, { name: 'David', age: 29 }, { name: 'Amy', age: 33 }, { name: 'Peter', age: 27 }, { name: 'Rachel', age: 31 }, { name: 'Thomas', age: 24 } ]; // Extract the first 5 users and add them to a new array let users2 = users.slice(0, 5); // Convert the array to an object let userObj = {}; users2.forEach((user, i) => { userObj[i+1] = user; });
gistlibby LogSnag