Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I not show the undefined values from map() return

I have an import from a json file with a random text and I have to get all the words in which the first letters are vowels.

let texts = MyData.map(text => {
return text.tags.map(word => {
return word.split(' ').map( char => {
   if(char[0].match(/[aeiou]/i)) {
    return char
   }
})
});
});
console.log(texts); [ example : 'ea',       'in',
  undefined,  'in',
  'occaecat', undefined,
  undefined ] 

So how can I make the return value be without the undefined values and in the same map function?

Sample data of MyData for anyone wondering :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "_id": "62bab08c83586a7bb36b46de",
    "index": 0,
    "tags": [
      "ea in minim in occaecat pariatur cillum",
      "ut exercitation minim officia enim cillum anim",
      "ad occaecat labore velit cupidatat enim proident",
      "consequat culpa qui occaecat sit sunt voluptate",
      "eiusmod excepteur adipisicing tempor ut Lorem do",
      "quis velit aliquip ad excepteur deserunt do",
      "dolor fugiat ea sit adipisicing labore in"
    ]
  },
  {
    "_id": "62bab08c10365bb88f81cdf5",
    "index": 1,
    "tags": [
      "non laborum cillum commodo velit culpa commodo",
      "nisi aute magna laborum ut cillum velit",
      "in veniam ullamco officia aute deserunt ex",
      "dolor ullamco aliqua laborum ullamco officia mollit",
      "fugiat aliquip nostrud deserunt fugiat veniam veniam",
      "culpa eu irure ullamco ea deserunt ullamco",
      "labore quis quis enim magna duis cupidatat"
    ]
  },
  {
    "_id": "62bab08cf0d1796087e71a27",
    "index": 2,
    "tags": [
      "esse labore aliqua sit voluptate mollit deserunt",
      "sint nulla minim veniam do nulla duis",
      "culpa enim laborum do magna voluptate amet",
      "commodo elit ad magna veniam proident duis",
      "aliquip ex dolore officia laboris sit qui",
      "esse anim velit ut quis do magna",
      "do culpa eu sint occaecat voluptate cillum"
    ]
  },
  {
    "_id": "62bab08cbb8247421f46d095",
    "index": 3,
    "tags": [
      "ea aliqua cupidatat aute ipsum qui officia",
      "enim ad pariatur ex tempor pariatur irure",
      "in mollit aute sit occaecat non cupidatat",
      "adipisicing sint non elit nisi commodo sunt",
      "tempor veniam culpa exercitation in cillum pariatur",
      "non quis dolor in ea ut duis",
      "excepteur Lorem dolor qui tempor dolore reprehenderit"
    ]
  },
  {
    "_id": "62bab08c642b523e6cc9ecd1",
    "index": 4,
    "tags": [
      "cillum nulla ipsum pariatur nisi ex in",
      "proident tempor aliquip id commodo sunt ut",
      "tempor qui ex laborum anim nisi excepteur",
      "consequat ea laborum cillum mollit enim consectetur",
      "nisi ut sint sunt non veniam ullamco",
      "proident exercitation culpa dolor duis enim qui",
      "commodo aliquip ipsum velit elit in nulla"
    ]
  },
  {
    "_id": "62bab08c5702bc78e0e082db",
    "index": 5,
    "tags": [
      "deserunt sit labore veniam eiusmod tempor eu",
      "ut dolore est pariatur eiusmod cillum eu",
      "ex commodo voluptate Lorem incididunt dolor veniam",
      "labore ullamco quis et qui fugiat pariatur",
      "in duis ut tempor velit excepteur ut",
      "aute esse velit nisi et reprehenderit ea",
      "consectetur do aute et dolor ex do"
    ]
  }
]

>Solution :

I think you need to use filter in inner most map function

let texts = MyData.map(text => {
    return text.tags.map(word => {
        return word.split(' ').filter( char => {
           if(char[0].match(/[aeiou]/i)) {
            return char
           }
        })
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading