How to convert an array of objects into an array of objects with mapped key-value pairs

osmancakirio

It is hard to put into words, so If anyone could help me with the title of the question, thanks in advance. At the end of the day, I have an array like so;

[ 
  {
    field: "firstname",
    value: "John"
  },
  {
    field: "lastname",
    value: "Doe"
  },
  {
    field: "hobbies",
    value: "singing, basketball"
  },
]

and the desired output will be like below;


[ 
  {
   "firstname":"John"
  },
  {
    "lastname":"Doe"
  },
  {
    "hobbies" :"singing, basketball"
  },
]

The closest question I could find similar to mine was this one: How to convert an array into an object in javascript with mapped key-value pairs?

Sakil

Use map and return object, where key will be obj.field and value as obj.value
i.e. {[obj.field]: obj.value}

let output = [ 
    {
        field: "firstname",
        value: "John"
    },
    {
        field: "lastname",
        value: "Doe"
    },
    {
        field: "hobbies",
        value: "singing, basketball"
    },
].map(a => ({[a.field]: a.value}))

console.log(output)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to convert an array of objects to object with key value pairs

how to convert array of objects to key value pairs in php

convert an array and an array of arrays into array of objects with key value pairs

How to convert an array into an object in javascript with mapped key-value pairs?

Convert array of objects to object of key-value pairs

Convert JSON with array of objects of unpredictable key-value-pairs to CSV

Convert array of objects to comma separated key value pairs javascript

Transform an array of key/value pairs into array of objects

How to parse an objects with array index to object array with key value pairs

How to convert Key Value list into array of Objects?

how to find a value in array of objects with key value pairs

How to convert object containing key-value pairs to an array of objects in JavaScript

Build an array of objects with two key/value pairs

Map array of objects to object of key value pairs

How do I turn key value pairs in object into array of objects?

Convert array to array of objects with key as same as value

How to convert an array of key–value arrays to an array of objects with .reduce function?

Convert array of single-property JavaScript objects to array of key/value pairs

How to covert an array of strings into objects so that I can split those objects into key value pairs?

JS - How to add key:value pairs from objects nested in arrays to other objects nested in another array

Convert an array of arrays to an array of objects with mapped properties

isolate key value pairs in array of objects that have null value

Convert nested array of objects to object of key-value pairs using reducer

In array of multiple objects and key value pairs, convert all number values to negative

How to convert array of objects into enum like key value pair in javascript?

How to convert object with key/value 's to Array of Objects?

Convert Array of Objects to Key Value Structure in Javascript

How to filter an array with complex nested objects based on another array of key/value pairs?