使用reduce 和 entries 实现数据类型的转化

 

 

<!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>

 

 

 

使用reduce 和 entries  实现数据类型的转化

 

上一篇:eclipse插件开发-添加xmlcatalog到plugin Specified Entries


下一篇:[JS]Object.keys, values, entries