0

starMap={
  B: 'COM',
  C: 'B',
  D: 'C',
  E: 'D',
  F: 'E',
  G: 'B',
  H: 'G',
  I: 'D',
  J: 'E',
  K: 'J',
  L: 'K',
  YOU: 'K',
  SAN: 'I'
};
const getAncestors = body => body in starMap ? [ ...getAncestors(starMap[body]), starMap[body] ] : [];
console.log(getAncestors("YOU"));

const getAncestors = body => body in starMap ? [ ...getAncestors(starMap[body]), starMap[body] ] : [];

i'll understand everything expect the extra starmap[body] in addition to the getancestor(starrmap[body])

pravin deva
  • 69
  • 2
  • 8
  • `body in starMap ? [ ...getAncestors(starMap[body]), starMap[body] ] : [];` means if there is a property called `body`(a string from the param) exists in `starMap` then it returns an array of items with whatever returned from `getAncestors(starMap[body])` & `starMap[body]`'s value or it returns an empty array – Beingnin Aug 15 '20 at 07:20
  • 1
    [ ...getAncestors(starMap[body]), starMap[body] ] is an array with all elements in getAncestors(starMap[body]) plus starMap[body] as last element. ie [...[1,2,3], 4] = [1,2,3,4] – Chris Li Aug 15 '20 at 07:24
  • https://stackoverflow.com/a/41640566/5456789 – Amir Azizkhani Aug 15 '20 at 08:37

1 Answers1

1

starMap is an object getAncestors is a function that returns true if the argument body is in the object starMap the ? operator means something like this

let tog = 1 == 1 ? 'Yeah it is' : 'Nope'

That's the same as

if(1==1){
  let tog = 'Yeah it is'
}
else {
  let tog = 'Nope'
}

and getAncestors = body => body in starMap is the same as

const getAncestors = (body) => {
  return body in starMap;  //true or false
}
Altro
  • 878
  • 1
  • 7
  • 23
  • 1
    _getAncestors is a function that returns true if the argument body is in the object starMap_, that's not correct, `getAncestors` returns an array. – Nick Parsons Aug 15 '20 at 07:47
  • 1
    i already understood the 'in' operator the think i dont understand is [ ...getAncestors(starMap[body]), starMap[body] ] additional starmap[body] – pravin deva Aug 15 '20 at 07:58