“JavaScript Rock” Código de respuesta

arrancar javascript

function pluck(arr, key) {
  return arr.map(obj => obj[key]);
}
Suman Majhi

JavaScript Rock

// Write a function that returns an array of only the objects with truthy values
// for the passed in key.
//
// pluckTruthy({a: 1, b: '', c: false}, 'a') => [1]
// pluckTruthy({a: 1, b: '', c: false}, 'b') => []
// pluckTruthy({a: 1, b: '', c: false}, 'c') => []
// pluckTruthy({a: 1, b: '', c: false}, 'd') => []
//
function pluckTruthy(obj, key) {
  var result = [];
  for (var i = 0; i < obj.length; i++) {
    if (obj[i][key]) {
      result.push(obj[i][key]);
    }
  }
  return result;
}

// console.log(pluckTruthy({a: 1, b: '', c: false}, 'a'));
Caffeinated Developer

Respuestas similares a “JavaScript Rock”

Preguntas similares a “JavaScript Rock”

Más respuestas relacionadas con “JavaScript Rock” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código