Perform Actions on all elements arrays(3)
image source: https://www.google.com/imgres?imgurl=https%3A%2F%2Fscriptverse.academy%2Fimg%2Ftutorials%2Fjs-array-map.png&imgrefurl=https%3A%2F%2Fscriptverse.academy%2Ftutorials%2Fjs-array-map.html&tbnid=CAhSYJdLg_NP0M&vet=12ahUKEwikpqewoa_sAhUK0KwKH

Perform Actions on all elements arrays(3)

There are a method incredible powerful call map that allow us to make transform in each element of the array.

//we have an array
const temperatures = [
  { degrees: 69, isRecordTemp: false },
  { degrees: 82, isRecordTemp: true },
  { degrees: 73, isRecordTemp: false },
  { degrees: 64, isRecordTemp: false }
];

//and suppose that we want to change Record Temp we want true in all of them, we can use map method

const newTemps ? temperatures.map(temp =>{

 temp.isRecordTemp = true
 return temp

})

//return
const temperatures = [
  { degrees: 69, isRecordTemp: true },
  { degrees: 82, isRecordTemp: true },
  { degrees: 73, isRecordTemp: true },
  { degrees: 64, isRecordTemp: true }

];

// that change the value and return as new array

so map is very special and powerful array method.

There are other powerful array method call forEach basically is the same that map but don't return a new array, simple modify the elements and save in the same array.

I hope that you learned a new thing, with love Bernardo Aguayo


要查看或添加评论,请登录

Bernardo Aguayo Ortega的更多文章

  • Get Subsets of arrays(4)

    Get Subsets of arrays(4)

    What if we only want a subset of data, no to transform the array we only want the elements that we are interesting on…

  • Check Element Existence in Arrays (2)

    Check Element Existence in Arrays (2)

    If you work with a hard code values is easy to work with them because you know where are the values inside the array…

  • Build Flexible Collections with Arrays (1)

    Build Flexible Collections with Arrays (1)

    There are a data structure to save collection of data call it array use [] brackets instead {}, Arrays are a kind of…

  • Merge Objects with Object Spread

    Merge Objects with Object Spread

    If you want to merge various objects you need to spread object's properties in new object and literary since ECMAScript…

  • Object Destructuring (JavaScript)

    Object Destructuring (JavaScript)

    Object destructuring allows us to pull properties from an object and make them into variables, in other words allow us…

  • Use Objects for Managing key-value pairs (js)

    Use Objects for Managing key-value pairs (js)

    We already know the different types of variables in JavaScript so you can see a variable as a box and an object like a…

    2 条评论
  • Shorter functions with arrow functions

    Shorter functions with arrow functions

    With ES6 we have a new form to write functions, that allow us to write shorter functions, we don't need any more the…

    4 条评论
  • How Functions Should Be Named

    How Functions Should Be Named

    A few things to remember when naming functions:Name functions after a verb which clearly conveys what they do Omit…

  • single responsibility functions

    single responsibility functions

    How to make your function only have one responsibility? As the name say it this principle talks about make functions…

  • Better functions with default parameters (javascript)

    Better functions with default parameters (javascript)

    Imagine the you have a function function convertTemperature(celsius , decimalPlaces) { ? const fahrenheit = celsius *…

    4 条评论

社区洞察

其他会员也浏览了