0

I have an array of array containg informations about the employe of a company. The array look like this

[
   [
      "14578",==> employee number
      "Marc", ==> employee name
      "Paris",==> employee city
      "ENGINEER",==> employee position
      "24578963", ==> employee salary
      "A"         ==> employee team
   ],
   [
      "81832",
      "Alan",
      "Marseille",
      "MECHANIC",
      "25578963",
      "C"
   ],
   [
      "39629",
      "John",
      "Lyon",
      "PLUMBER",
      "26578963",
      "F"
   ]
]

And i want to each array , to get the first information (employee number ) and the fourth information (employee position)

desired output
[{"employee number": "14578","employee position": "ENGINEER"}, {"employee number": "81832","employee position": "MECHANIC"}, {"employee number": "39629","employee position": "PLUMBER"}]

I use map like this :

const myExtractArray = myInitialArray.map( elt =>
       {
       "employee number" : elt[0],
       "employee number":elt[3]
       return elt
       }
    );

But i doesn't get the desired output. How can i do this please ?

peace3106
  • 37
  • 4
  • 1
    `retur elt` is invalid syntax. a) there's no such thing as `retur`, b) you cannot "return" something in an object, c) if you use an arrow function then `{ }` on its own is treated as a block and not an object, for that you would have to wrap it in `( )`, hence those "properties" should/would also cause a `SyntaxError` – Andreas Aug 25 '22 at 09:50
  • Solution is as below. @Andreas has well explained in the above comment. const myExtractArray = myInitialArray.map( elt => ({ "employee number" : elt[0], "employee position":elt[3] }) ); – sasi k Aug 25 '22 at 09:57

1 Answers1

2

You're not returning anything from the map().

Also retur is a syntax error.

const data = [["14578", "Marc", "Paris", "ENGINEER", "24578963", "A"], ["81832", "Alan", "Marseille", "MECHANIC", "25578963", "C"], ["39629", "John", "Lyon", "PLUMBER", "26578963", "F"] ];

const result = data.map(o => {
    return {
        "employee number": o[0],
        "employee position": o[3]
    };
});

console.log(result);
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Why is this not a dupe of [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript-6-arrow-function-that-returns-an-object)? – Andreas Aug 25 '22 at 09:52