数据之间的转换

对象转数组

let obj = {
        name: "zhangsan",
        age: 1
    }
    let arr = [].slice.call(obj)
    let arr1 = Array.from(obj)
    console.log(arr)
    console.log(arr1)

image

数组转字符串

    let arr = [1, 23, 4, 5, 6, 7, 8, 9];
    let obj = arr.join(",");
    let obj1 = arr.toString();
    console.log(obj)
    console.log(obj1)

image

字符串转数组

    let str = "fiewuhfiwuehi";
    let arr = str.split();
    let arr1 = str.split(",");
    let arr2 = Array.from(str);
    let arr3 = [str];
    let arr4 = [...str]
    console.log(arr);
	Array(1)0: "fiewuhfiwuehi"length: 1[[Prototype]]: Array(0)

    console.log(arr1);
	Array(1)0: "fiewuhfiwuehi"length: 1[[Prototype]]: Array(0)

    console.log(arr2);
	Array(13)0: "f"1: "i"2: "e"3: "w"4: "u"5: "h"6: "f"7: "i"8: "w"9: "u"10: "e"11: "h"12: "i"length: 13[[Prototype]]: Array(0)

    console.log(arr3);
	Array(1)0: "fiewuhfiwuehi"length: 1[[Prototype]]: Array(0)

    console.log(arr4);
	Array(13)0: "f"1: "i"2: "e"3: "w"4: "u"5: "h"6: "f"7: "i"8: "w"9: "u"10: "e"11: "h"12: "i"length: 13[[Prototype]]: Array(0)
posted @ 2022-03-23 15:36  sprite692  阅读(37)  评论(0)    收藏  举报