Let’s start with the title:

Convert arR to {0: ‘male ‘, 1:’ female ‘}

Const arr = [{label: 'male ', value: 0}, {label:' female ', value: {1},] the function f (arr) / / your code} const obj = f (arr) console. The log (obj) = = > {0: 'male', 1: 'female'}Copy the code

Method 1 (simpler and more direct)

Function f(arr){// Your code const obj = {} arr.foreach (item => {obj[item.value] = item.label}); } const obj = f (arr) console. The log (obj) = = > {0: 'male', 1: 'female'}Copy the code

Method 2 (Using reduce method)

Function f(arr) {// Write code // reduce return arr.reduce((acc, item) => {acc[item.value] = item.label return acc}, {})} const obj = f (arr) console. The log (obj) = = > {0: 'male', 1: 'female'}Copy the code

You can also write code on a single line

function f(arr){ return arr.reduce((acc,item) => ({... acc,[item.value]:item.label}),{}) }Copy the code