0

Hello i'm in a JS map from a api response. i have multiple object like this :

{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type1', label: "Pieds dans l'eau"}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type3', label: 'Foodies'}
{value: 'type1', label: "Pieds dans l'eau"}
{value: 'type1', label: "Pieds dans l'eau"}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type4', label: 'Grand air & Nature'}
{value: 'type1', label: "Pieds dans l'eau"}
{value: 'type2', label: 'Patrimoine & Culture'}
{value: 'type3', label: 'Foodies'}

And i need a simple object like this for build a select :

const arr = {
    type1 : 'Pieds dans l\'eau',
    type2 : 'Patrimoine & Culture',
    type3 : 'Foodies',
    type4 : 'Grand air & Nature',
};

many thank for you help !

Paul-H
  • 13
  • 2
  • Have you tried looping over your array and setting properties on your object? What didn't work as expected? – David Dec 12 '22 at 13:47
  • use `array.reduce` – Ibrahim Hammed Dec 12 '22 at 14:00
  • So actually the OP just needs to create a map or object of `key`-`value` entries, where the value of `value` acts as `key` and the value of `label` acts as `value`. Btw. calling the expected result `arr` hints that the OP thinks of this structure as array but what the OP actually does assign is an object of key-value pairs. – Peter Seliger Dec 12 '22 at 14:00
  • Assuming `responseData` being an array of objects like shown by the OP ... `const lookup = responseData.reduce((result, { value, label }) => Object.assign(result, { [ value ]: label }), {} );` – Peter Seliger Dec 12 '22 at 14:24

0 Answers0