Js conversion between objects and arrays

There is a way to solve the ultimate problem of conversion arrays and objects

The following two examples are es6 syntax.

  1. It is converted into an array of objects:
    knowledge: Extended operator ...
let arr = [1,2,3];
let obj = {...arr};
console.log(obj); //{0: 1, 1: 2, 2: 3}
  1. Objects into an array
    of knowledge points: Object.values (obj)
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(obj)); // ['a', 'b', 'c']

Guess you like

Origin www.cnblogs.com/blogwxb/p/12567145.html