“Méthode de carte” Réponses codées

Fonction de cartographie

const arr = [{name: "test"}, {name: "test1"}, {name: "test2"}]

arr.map((n, i) => {
	console.log(n);
})
Code Sir

Comment utiliser la méthode de la carte en javascript

const numbers = [1, 2, 3, 4, 5]; 

const bigNumbers = numbers.map(number => {
  return number * 10;
});
TechWhizKid

carte()

The map() method creates a new array populated with the results of calling 
a provided function on every element in the calling array.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
CodeMaster64

Méthode de carte

function map(array, transform) {
  let mapped = [];
  for (let element of array) {
    mapped.push(transform(element));
  }
  return mapped;
}

let rtlScripts = SCRIPTS.filter(s => s.direction == "rtl");
console.log(map(rtlScripts, s => s.name));
// → ["Adlam", "Arabic", "Imperial Aramaic", …]
Xanthous Xenomorph

Carte en javascript

let myMap = new Map()

let keyString = 'a string'
let keyObj    = {}
// setting the values
myMap.set(keyString, "value associated with 'a string'")
myMap.set(keyObj, 'value associated with keyObj')
myMap.set(keyFunc, 'value associated with keyFunc')
myMap.size              // 3
// getting the values
myMap.get(keyString)    // "value associated with 'a string'"
myMap.get(keyObj)       // "value associated with keyObj"
myMap.get(keyFunc)      // "value associated with keyFunc"
Vishal

carte()

const myMap = new Map();
let userid = 1314124141;

myMap.set('veri_' + userid, 1) // set
myMap.set('veri_' + userid, myMap.get('veri_' + userid) + 1) // push

console.log(myMap.get('veri_' + userid)) // output: 3
Pudochu

Réponses similaires à “Méthode de carte”

Questions similaires à “Méthode de carte”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code