Google News
logo
CoffeeScript - Interview Questions
How to map an array in CoffeeScript?
Array in CoffeeScript : Coffeescript array and array object is very similar to JavaScript array and object of array, objects may be created using curly braces or may not be its depends on programmer choice.
 
Example of Array : 
name = ["sarthak","surabhi", "tejal",
        "dipali", "girija", "devendra"] 

department = {
   id : 10,
   branch : "computer"
}

skills = 
    designer :
         name : "ali"
         surname : "bazzi"
   backend :
         name : "sunny"
          surname : "warner"
 
Map array in CoffeeScript : Array map() is used when we want to transformed each value of the array and want to get new array out of it. The map is just used to map or track the value of the array
 
Example : In the below example we have an array of objects with different values in form of key-value pair and we apply the map function on that array to get a specific object’s value. In short, we want to transform an array to get a new array out of it. 
engineers = [
  { name : "ali" , surname : "bazzi"},
  { name : "virat" , surname : "sharma"},
  { name : "sharma" , surname : "pandey"},
  { name : "paresh" , surname : "vikramadity"},
  { name : "sandip" , surname : "jain"}
]
   
names_record = engineers.map(firstname) -> firstname.name
console.log(names_record)
Output:

['ali', 'virat', 'sharma', 'paresh', 'sandip'] 
Advertisement