将对象转化为数组

将对象转化为数组

  • 方法一:

    1
    2
    3
    4
    5
    6
    7
    let obj={a:1,b:2}
    let arr = []
    let x=Object.entries(obj)//[["a", 1],["b", 2]]
    arr=x.map(item=>{
    return item[1]
    })
    console.log(arr)//[1,2]
  • 方法二:

    1
    2
    3
    4
    5
    6
    7
    let obj={a:1,b:2}
    let arr = []
    for (let i in obj) {
    arr.push(obj[i]); //属性
    //arr.push(obj[i]); //值
    }
    console.log(arr);//[1,2]
  • 方法三:

    1
    2
    3
    let obj={a:1,b:2}
    let arr = Object.values(obj)
    console.log(arr);//[1,2]
您的支持将鼓励我继续创作!