<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> let obj = { a:1, b:2, c:3, d:4 } // [{text:a ,value:1},{text:b ,value:2},{text:c ,value:3},{text:d ,value:4}] console.log(Object.entries(obj)) //[[a:1],[b:2]]..... let result = Object.entries(obj).map(item =>{ return {text:item[0],value:item[1]} }) console.log(result)// [{text:a ,value:1},{text:b ,value:2},{text:c ,value:3},{text:d ,value:4}] </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> let obj = [{text:'a' ,value:1},{text:'b' ,value:2},{text:'c' ,value:3},{text:'d' ,value:4}] // let obj = { // a:1, // b:2, // c:3, // d:4 // } let result = obj.reduce((pre,cur,index)=>{ pre[cur.text] = cur.value return pre },{}) console.log(result) //{a: 1, b: 2, c: 3, d: 4} </script> </body> </html>