Home > Software engineering >  Trying to construct an array from a known map function
Trying to construct an array from a known map function

Time:11-03

I trying to construct a docs array that would work on docs.maps() function below. However when I run it I get

TypeError: Cannot read property 'uuid' of undefined

If I do

console.log(docs[1]['_source'].uuid);

I get 2 as expected.

Question

Can anyone figure out why the map function can't access uuid but console.log() can?

const docs = [{'_id': 1},{'_source': {'uuid': 2}},{'_source': {'@timestamp': 3}}];

const description = dedent`
================================================
${
  docs.map((element) => [
    `Log entry ID: ${element['_id']}`,
    `UUID: ${element['_source'].uuid}`,
    `Timestamp: ${element['_source']['@timestamp']}\n`,
  ].join('\n')).join('--------------------\n')
}
`;

console.log(description);

CodePudding user response:

You have to structure like so

const docs = [{'_id': 1,'_source': {'uuid': 2,'@timestamp': 3}}];
  • Related