0

'use strict';

const characters = [
  { id: 1, name: 'ironman', env: 'marvel' },
  { id: 2, name: 'black_widow', env: 'marvel' },
  { id: 3, name: 'wonder_woman', env: 'dc_comics' },
];

function hasCharacterFrom(env) {
  return character => character.env === env;
}

console.log(characters.find(hasCharacterFrom('marvel')));
// { id: 1, name: 'ironman', env: 'marvel' }

console.log(characters.some(hasCharacterFrom('marvel')));
// true

I am not able to understand how the function hasCharacters() works internally. Its neither an arrow function, nor a ternary operation been used there. How is the =》 used here?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Arpan Banerjee
  • 826
  • 12
  • 25
  • 1
    Hi, to understand this code you could read more about anonymous arrow functions and higher order functions in javascript – Marek Szkudelski Mar 27 '20 at 14:26
  • 1
    There is no such function as `hasCharacters`. The function `hasCharacterFrom` *returns* a function which **is** an arrow function (and the `env` variable is closed over). – Quentin Mar 27 '20 at 14:28
  • 1
    `return character => character.env === env`; is `return function (character) { return character.env === env }`.... Any clearer? So it returns a function.... – epascarello Mar 27 '20 at 14:29
  • 1
    Also checkout [What is 'Currying'?](https://stackoverflow.com/questions/36314/what-is-currying) – palaѕн Mar 27 '20 at 14:30
  • yes i knw there is no inbuilt function hasCharacters() i just wanted to know how is it implemented, the logic behind it. – Arpan Banerjee Mar 27 '20 at 14:38
  • how it gets access to the characters object inside the hasCharacter() function, where are we passing it as a prameter? i can see we are only passing "env" as a parameter – Arpan Banerjee Mar 27 '20 at 14:59
  • ohh, i think i got it-- characters.find(hasCharacters("marvel") here we are going through each character of characters object, just like array.forEach() . pls correct me if i am wrong – Arpan Banerjee Mar 27 '20 at 15:02

0 Answers0