Skip to content

Instantly share code, notes, and snippets.

@debbysa
Last active October 3, 2021 22:47
Show Gist options
  • Save debbysa/b5974a85c7b036b360088b8dc5af337b to your computer and use it in GitHub Desktop.
Save debbysa/b5974a85c7b036b360088b8dc5af337b to your computer and use it in GitHub Desktop.
const student = [
{ id: 11, name: "Smith" },
{ id: 12, name: "Roger" },
{ id: 13, name: "Romeo" },
{ id: 14, name: "Jesicca" },
{ id: 15, name: "Ellen" }
];
// // what we need ['smith', 'Roger', 'Romeo', 'Jesicca','Ellen']
// // using foreach
// need to create array variable
var studentName = [];
student.forEach((item) => {
studentName.push(item.name);
});
console.log("pake forEach = ", studentName);
// if using Map
const studentNameMap = student.map((item) => item.name);
console.log("pake map = ", studentNameMap);
// implementasi lainnya
const b = [];
b.forEach((item) => console.log(item)) // return undefined
b.map((item) => console.log(item)) // return [undefined, undefined, undefined, undefined]
const a = [1, 2, 3, 4]
a.forEach((item) => item + 1) // undefined
a.map((item) => item + 1) // return [2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment