[javascript] Ask when returning an object in the map function

Asked 2 years ago, Updated 2 years ago, 56 views

Hello. The map function below is A function that only gets the first, last name from the inventors.



    const inventors = [
      { { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
      { { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
      { { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
      { { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
      { { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
      { { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
      { { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
      { { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
      { { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
      { { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
      { { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
      { { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
    ];

   const inventorsName1 = inventors.map( inventor => {
        return  {inventor["first"] , inventor["last"]}; // Uncaught SyntaxError: Unexpected token [
    });

    const inventorsName2 = inventors.map( inventor => {
        var obj = {};
        obj["first"] = inventor.first;
        obj["last"] = inventor.last;
        return obj    
    });

I think both inventorName1 and inventorName2 return the same object. Why do I get an error if I do it the same way as 1?

Thank you for reading my question!

javascript map

2022-09-22 18:00

2 Answers

Ah... It's a self-answer again (Crying) I'm a {} type, so can't I return it? I've been thinking... As expected, there is no error in grammar. It was because I marked it wrong.

   const inventorsName1 = inventors.map( inventor => {
        return  {inventor["first"] , inventor["last"]}; // Uncaught SyntaxError: Unexpected token [
    });

The return object in has only the value and no key.

So...

 const inventorsName1 = inventors.map( inventor => {
        return { first : inventor.first , last : inventor.last}
    });

You must modify it to .

By the way, the map function and the filter function go around the factor of the callback function and store the return value whenever the first factor (inventor here) changes (in this case, the obj object where first and last are stored) and output it in an array format to the inventorName variable.

Thank you for reading my questions and answers.

The reason why I was confused is that the map function returns around each element, how is it stored and then stored in a variable at once? I don't understand. If you just want to lump and execute commands collectively in an array, do the map function! I think that's why I moved on. I don't know if you understood it right now. I don't know ㅠ 혹시 I would appreciate it if you could give me some feedback!


2022-09-22 18:00

Hello!

The reason I was confused is because the map function is going around each element It returns how it is stored in the variable at once You can save it, right?

function NaiveMap() {
  let index = -1
  const length = array == null ? 0 : array.length
  const result = new Array(length)

  while (++index < length) {
    result[index] = func(array[index], index, array)
  }
  return result
}

Of course, the map function is written in c++ The Google v8 engine test code also has a loose map function in JavaScript.

I hope the question has been resolved a little bit!


2022-09-22 18:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.