js 对象,数组,字符串,相互转换

1:对象转换数组

  let obj = {'val1':1, 'val2':2, 'val3':3, 'val4':4};

  var arr = []

  for (let i in obj) {  //取键

    let o = {};

    o[i] = obj[i];  //寻找键值对赋值

    arr.push(o)

  }

  console.log(arr);  //[ { val1: 1 }, { val2: 2 }, { val3: 3 }, { val4: 4 } ]

2:数组转字符串

  let a = [123,432,42,5,344,5]

  1) toString()

    console.log(a.toString())  //123,432,42,5,344,5

  2)  join('')

    console.log(a.join('-'))  //123-432-42-5-344-5

3:字符串转数组

  let str = 'daw2312-daw=+'

  console.log(str.split(''))  //[ 'd', 'w', '2', '2', '-', 'a', 'w', '=' ]

上一篇:uniapp 开发微信小程序总结(四)mixin - login


下一篇:SPOJ 370 Ones and zeros BFS + 同余剪枝