配列使用経験例について

21604 ワード

配列使用例
  • は、削除配列の複数の要素
  • を指定する.
  • 配列文字列-回転数
  • 配列と文字列-相互変換
  • は、idに基づいてツリー構造データ中の完全経路
  • を取得する.
    配列から削除する複数の要素を指定します.
    例:[1,2,3,4,5,6,7]配列から2,4,5,7の4つの要素を削除します.
    let arr1 = [ 1,2,3,4,5,6,7];
    let arr2 = [2,4,5,7]; 
    for(let obj of arr2){
    	arr1 = arr1.filter(item => item !== obj);
    }
    console.log(arr1);
    //   :[1,3,6]
    
    配列文字列-回転数
    //    
    let arr = ["2","4","6","8","10","13"]
    let arr2 = arr.map(item => {
    	return +item
    });
    console.log(arr2)
    // [2, 4, 6, 8, 10, 13]
    
    //    
    let arr2 = ["2","4","6","8","10","13"].map(Number);
    console.log(arr2)
    // [2, 4, 6, 8, 10, 13]
    
    配列と文字列→相互変換
  • 配列変換文字列
  • //  ‘-’         
    let str = [0,1,2,3,4].join('-');
    console.log(str);
    // 0-1-2-3-4
    
  • 文字列変換配列
  • //    (,)    ,        
    let str = "hello,wworld,hi"
    let arr = str.split(',');
    console.log(arr);
    // ["hello", "wworld", "hi"]
    
    IDに基づいてツリー構造データのフルパスを取得します.
    //   
    let arr = [{
        "id": 1,
        "name": "    ",
        "level": 1,
        "fullPath": "1",
        "parentId": 0,
        "children": [{
            "id": 2,
            "name": "    ",
            "level": 2,
            "fullPath": "1-2",
            "parentId": 1,
            "children": [{
                "id": 3,
                "name": " ",
                "level": 3,
                "fullPath": "1-2-3",
                "parentId": 2
            }, {
                "id": 26,
                "name": "  ",
                "level": 3,
                "fullPath": "1-2-26",
                "parentId": 2
            }, {
                "id": 27,
                "name": "  ",
                "level": 3,
                "fullPath": "1-2-27",
                "parentId": 2
            }, {
                "id": 28,
                "name": "  ",
                "level": 3,
                "fullPath": "1-2-28",
                "parentId": 2
            }, {
                "id": 29,
                "name": "   ",
                "level": 3,
                "fullPath": "1-2-29",
                "parentId": 2
            }, {
                "id": 38,
                "name": "  ",
                "level": 3,
                "fullPath": "1-2-38",
                "parentId": 2
            }]
        }, {
            "id": 10,
            "name": "    ",
            "level": 2,
            "fullPath": "1-10",
            "parentId": 1,
            "children": [{
                "id": 11,
                "name": "   ",
                "level": 3,
                "fullPath": "1-10-11",
                "parentId": 10
            }, {
                "id": 14,
                "name": "    ",
                "level": 3,
                "fullPath": "1-10-14",
                "parentId": 10
            }, {
                "id": 17,
                "name": "  ",
                "level": 3,
                "fullPath": "1-10-17",
                "parentId": 10
            }, {
                "id": 20,
                "name": "   ",
                "level": 3,
                "fullPath": "1-10-20",
                "parentId": 10
            }, {
                "id": 23,
                "name": "  ",
                "level": 3,
                "fullPath": "1-10-23",
                "parentId": 10
            }]
        }]
    }]