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 extract and pair the values of an array based object

I’m trying to create a String based upon an object consisting of several key-value pairs.

Example:

  [{ name: 'cookie1', value: 'false' },
  { name: 'cookie2', value: '123' },
  { name: 'cookie3',value: 'abc'}]

What I’m trying to achieve (string):

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

cookie1: false, cookie2: 123, cookie3: abc

I’ve tried to extract just the val using map like this (played around moving around values):

var output = cookies.map(d => {
  return {
    "name": d.name,
    "value": d.value, 
  }
})

>Solution :

One way to do this is to map the array of objects into name: value strings and then join them with , :

const data =   [{ name: 'cookie1', value: 'false' },
  { name: 'cookie2', value: '123' },
  { name: 'cookie3',value: 'abc'}]
  
const result = data.map(({ name, value }) => `${name}: ${value}`).join(', ')

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