Object.entries,Object.fromEntries 常用方法以及简易实现

时间不在于你拥有多少,而在于你怎样使用 ------LOL艾克

appetizer:奇怪的JS

const a = ("b"+"a"+ +"a"+"a").toLowerCase()
console.log(a) // banana  

start

官方释义MDN

Object.entries():方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用for...in语句以任意顺序遍历一个对象的除Symbol以外的可枚举属性。") 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环还会枚举原型链中的属性)。(ES7)
Object.fromEntries(): 方法把键值对列表转换为一个对象。(ES10)

Object.entries例子
// 转换
console.log(Object.entries({ name: 'JAY', age: 41 })); 
//[ [ 'name', 'JAY' ], [ 'age', 41 ] ]

console.log(Object.entries([1,2,3,4])) 
//[ [ '0', 1 ], [ '1', 2 ], [ '2', 3 ], [ '3', 4 ] ]

console.log(Object.entries('banana'))  
//[ [ '0', 'b' ],[ '1', 'a' ],[ '2', 'n' ],[ '3', 'a' ],[ '4', 'n' ],[ '5', 'a' ] ]

console.log(Object.entries(1)) //传入数字和浮点数返回 []

// 将OBject 转换为map
const obj = { name: 'JAY', age: 41 };
const map = new Map(Object.entries(obj))
console.log(map) 
// Map { 'name' => 'JAY', 'age' => 41 }
Object.fromEntries 列子
// node v10.16.3环境还未有支持,请在谷歌49+版本呢调试
// 转化map
const map = new Map([
  ['name','周杰伦'],['age',36],
])

console.log(Object.fromEntries(map)) 
// {name: "周杰伦", age: 36}

// 转化array
const arr = [
  ['0', 'a'],
  ['1', 'b'],
  ['2', 'c']
];
console.log(Object.fromEntries(arr)) 
// {0: "a", 1: "b", 2: "c"}

// 将对象中的属性数值统一*n
const obj1 = {a:1,b:2,c:3,d:'isD'}
const obj2 = Object.fromEntries(
  Object.entries(obj1).map(([key,val])=>{
    if(typeof val === 'number')return [key,val*2]
    else return [key,val]
  })
)
console.log(obj2) 
//{a: 2, b: 4, c: 6, d: "isD"}

// format url 

const str = "age=zhoujielun&&name=16"
const ar = new URLSearchParams(str)
console.log(Object.fromEntries(ar)) 
//{age: "zhoujielun", name: "16"}

API MDN地址

Object.entries

Object.fromEntries

URLSearchParams

API兼容性

Object.fromEntries
Object.entries
URLSearchParams

简易实现

Object.entries
const entries = (inArg)=>{
  if(Array.isArray(inArg)){
    return inArg.map((x,index)=>[`${index}`,x])
  }
  if(Object.prototype.toString.call(inArg) === `[object Object]`){
   return Object.keys(inArg).map(y=>[y,inArg[y]])
  }
  if(typeof inArg === 'number')return []
  throw 'Cannot convert argument to object'
} 

// test
console.log(entries(1)) 
// []

console.log(entries([1,2,3])) 
// [ [ '0', 1 ], [ '1', 2 ], [ '2', 3 ] ]

console.log(entries({age:12,name:'ysss'})) 
// [ [ 'age', 12 ], [ 'name', 'ysss' ] ]

if(!Object.entries)Object.entries = entries;

Object.fromEntries

const fromEntries = (arrArg)=>{
  // Map
  if (Object.prototype.toString.call(arrArg) === '[object Map]') {
    const resMap = {};
    for (const key of arrArg.keys()) {
      resMap[key] = arrArg.get(key);
    }
    return resMap;
  }
  // array
  if(Array.isArray(arrArg)){
    const resArr = {}
    arrArg.map(([key,value])=>{
      resArr[key] =  value
    })
    return resArr
  }
  throw 'Uncaught TypeError: argument is not iterable';
}

// test 
const map = new Map([
  ['name', '周杰伦'],
  ['age', 36]
]);
console.log(fromEntries(map));
 // {name: "周杰伦", age: 36}

const arr = [
  ['0', 'a'],
  ['1', 'b'],
  ['2', 'c']
];
console.log(fromEntries(arr)) 
// {0: "a", 1: "b", 2: "c"}

// polyfill
if(!Object.fromEntries)Object.fromEntries = fromEntries 


just

昨天没睡好有些慵懒所以开始总结一些小知识,如果有帮助到的请点个,谢谢 , 写的匆忙如有错误请指正。

你可能感兴趣的:(Object.entries,Object.fromEntries 常用方法以及简易实现)