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 to filter object array and return only 2 of 4 properties and formated?

I receive from API the following object array:

{id: 0, color: 'Red', code:'AAA'}
{id: 1, color: 'Blue', code:'BBB'}
{id: 2, color: 'Orange', code:'CCC'}
{id: 3, color: 'Black', code:'DDD'}

how can I return it for another array but only if id is > 1 and like this:

const arrayFiltered = { 'Orange#CCC', 'Black#DDD' }

I need convert it in a string array and concatenate 2 atributes…

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

>Solution :

Assuming given array is givenArr

const givenArr = [
  {id: 0, color: 'Red', code:'AAA'},
  {id: 1, color: 'Blue', code:'BBB'},
  {id: 2, color: 'Orange', code:'CCC'},
  {id: 3, color: 'Black', code:'DDD'}
]

result = givenArr.filter((val) => val.id > 1).map((item) => `${item.color}#${item.code}`)

console.log(result)
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