简体   繁体   中英

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):

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, 
  }
})

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM