javascript - write a function that returns an array of all the values of all the keys it corresponds to -
i want create 1 function can called return array containing values of each object.  e.g. function find ("firstname")  returns ["virginia", "zadie", "jane", "bell"]
an explanation great too, i'm incredibly new programming , trying learn. thank you
var writers = [   {     firstname: "virginia",     lastname: "woolf",     occupation: "writer",     age: 59,     alive: false   },   {     firstname: "zadie",     lastname: "smith",     occupation: "writer",     age: 41,     alive: true   },   {     firstname: "jane",     lastname: "austen",     occupation: "writer",     age: 41,     alive: false   },   {     firstname: "bell",     lastname: "hooks",     occupation: "writer",     age: 64,     alive: true   }, ];      
you can create function using map() method.
var writers = [ { firstname: "virginia", lastname: "woolf", occupation: "writer", age: 59, alive: false }, { firstname: "zadie", lastname: "smith", occupation: "writer", age: 41, alive: true }, { firstname: "jane", lastname: "austen", occupation: "writer", age: 41, alive: false }, { firstname: "bell", lastname: "hooks", occupation: "writer", age: 64, alive: true }];    function get(key, arr) {    return arr.map(function(e) {      return e[key]    })  }    console.log(get('firstname', writers))  console.log(get('occupation', writers))  
Comments
Post a Comment