javascript配列コピー

44969 ワード

javascript配列コピー
  • ケース1(aとbはともに配列):
    // var  
    var a = [1, 1, 1];
    var b = [...a];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
    
    // let  
    let a = [1, 1, 1];
    let b = [...a];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
    
    // const  
    const a = [1, 1, 1];
    const b = [...a];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a === b);	// false
    
  • ケース2(aは対象、bは配列):
    // var  
    
    // case1
    var a = {
           
        state: [1, 1, 1]
    }
    // !!!    
    var b = [...a.state];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    // case2
    var a = {
           
        state: [1, 1, 1]
    }
    // !!!    
    var {
           state: b} = a;
    
    console.log(b);		// `b`: [1, 1, 1]
    // !!!
    console.log(a);		// !!!? `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// !!! `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    
    // let   
    
    // case1
    let a = {
           
        state: [1, 1, 1]
    }
    let b = [...a.state];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    // case2
    let a = {
           
        state: [1, 1, 1]
    }
    let {
           state: b} = a;
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    
    // const  
    
    // case1
    const a = {
           
        state: [1, 1, 1]
    }
    const b = [...a.state];
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 1, 1]
    console.log(a.state === b);	// false
    
    // case2
    const a = {
           
        state: [1, 1, 1]
    }
    const {
           state: b} = a;
    
    console.log(b);		// `b`: [1, 1, 1]
    console.log(a);		// !!!? `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    b[1] = 0;
    console.log(b);		// `b`: [1, 0, 1]
    console.log(a);		// `a`: [1, 0, 1]
    console.log(a.state === b);	// true
    
    
  • ケース3(aとbはともに対象):
    // var  
    
    // case1
    var a = {
           
        state: [1, 1, 1]
    }
    
    var b = {
           };
    b.state = [...a.state];
    
    console.log(b.state);		// `b.state`: [1, 1, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    b.state[1] = 0;
    console.log(b.state);		// `b.state`: [1, 0, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    // case2
    var a = {
           
        state: [1, 1, 1]
    }
    
    var b = {
           };
    ({
           state: b.state} = a);
    
    console.log(b.state);	// `b.state`: [1, 1, 1]
    //    a.state  [1, 1, 1]
    console.log(a.state);	// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// true
    
    b.state[1] = 0;
    console.log(b.state);	// `b.state`: [1, 0, 1]
    console.log(a.state);	// `a.state`: [1, 0, 1]
    console.log(a.state === b.state);	// true
    
    
    // let  
    
    // case1
    let a = {
           
        state: [1, 1, 1]
    }
    
    let b = {
           };
    b.state = [...a.state];
    
    console.log(b.state);		// `b.state`: [1, 1, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    b.state[1] = 0;
    console.log(b.state);		// `b.state`: [1, 0, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    // case2
    let a = {
           
        state: [1, 1, 1]
    }
    
    let b = {
           };
    ({
           state: b.state} = a);
    
    console.log(b.state);	// `b.state`: [1, 1, 1]
    console.log(a.state);	// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// true
    
    b.state[1] = 0;
    console.log(b.state);	// `b.state`: [1, 0, 1]
    console.log(a.state);	// `a.state`: [1, 0, 1]
    console.log(a.state === b.state);	// true
    
    
    // const  
    
    // case1
    const a = {
           
        state: [1, 1, 1]
    }
    
    const b = {
           };
    b.state = [...a.state];
    
    console.log(b.state);		// `b.state`: [1, 1, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    b.state[1] = 0;
    console.log(b.state);		// `b.state`: [1, 0, 1]
    console.log(a.state);		// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// false
    
    // case2
    const a = {
           
        state: [1, 1, 1]
    }
    
    const b = {
           };
    ({
           state: b.state} = a);
    
    console.log(b.state);	// `b.state`: [1, 1, 1]
    console.log(a.state);	// `a.state`: [1, 1, 1]
    console.log(a.state === b.state);	// true
    
    b.state[1] = 0;
    console.log(b.state);	// `b.state`: [1, 0, 1]
    console.log(a.state);	// `a.state`: [1, 0, 1]
    console.log(a.state === b.state);	// true