美文网首页
判断对象是否为空对象以及把对象转换为数组的方法

判断对象是否为空对象以及把对象转换为数组的方法

作者: Light_shallow | 来源:发表于2018-07-25 13:28 被阅读0次

一、判断对象是否为空

1.将json对象转化为json字符串

JSON.stringify({}) == "{}"

2.ES6的Object.keys()方法

var arr =Object.keys({});

if(arr.length ==0){

console.log('这是一个空对象')

}

二、把对象转换为数组

let arrayLike = {

    '0':'a',

    '1':'b',

    '2':'c',

    length:8

  };

// ES5 的写法

        var arr = [].slice.call(arrayLike);// ['a', 'b', 'c', empty*5]

        console.log(arr[0]);//a

        console.log(arr[4]);//undefined

// ES6 的写法

let arr1 =Array.from(arrayLike); 

console.log(arr1);//["a", "b", "c", undefined, undefined, undefined, undefined, undefined]

扩展运算符(...)也可以将某些数据结构转为数组。

相关文章

网友评论

      本文标题:判断对象是否为空对象以及把对象转换为数组的方法

      本文链接:https://www.haomeiwen.com/subject/oyqpmftx.html