代码之家  ›  专栏  ›  技术社区  ›  Ja9ad335h

内部循环的javascript问题

  •  0
  • Ja9ad335h  · 技术社区  · 7 年前

    我尝试循环3个数组并生成一组具有唯一ID的子数组

    但出于某种原因财产 id 未获取唯一值(请参阅控制台结果)

    我试过各种各样的方法 forEach ,使用 closures ,使用了局部变量。它们都不起作用。

    const alpha = [{ name: 'AAA', value: 'a' }, { name: 'BBB', value: 'b' }, { name: 'CCC', value: 'c' }], 
    	numeric = [{ name: 'ONE', value: '1' }, { name: 'TWO', value: '2' }, { name: 'THREE', value: '3' }], 
    	symbol = [{ name: 'AT', value: '@' }, { name: 'HASH', value: '#' }, { name: 'DOLLAR', value: '$' }];
    
    const result = alpha.map(a => {
      a.children = numeric.map(n => {
        n.children = symbol.map(s => {
          s.id = a.value + n.value + s.value;
          return s;
        });
        n.id = a.value + n.value;
        return n;
      });
      a.id = a.value;
      return a;
    })
    
    
    console.log(result[0].children[0].children[0].id)   // should be "a1@"
    console.log(result[1].children[0].children[0].id)   // should be "b1@"
    console.log(result[2].children[0].children[0].id)   // should be "c1@"
    2 回复  |  直到 7 年前
        1
  •  2
  •   Ben Blank Jarret Hardie    7 年前

    问题是,您正在修改输入对象,因此相同(例如) s 正在多次修改。这就是为什么你只看到最后一个 alpha numeric 在您的ID中,它们是最后运行的,最后修改共享对象的也是最后运行的。

    要修复此问题,请返回 新的 对象和您的ID,所有的都应该是好的:

    const alpha = [{ name: 'AAA', value: 'a' }, { name: 'BBB', value: 'b' }, { name: 'CCC', value: 'c' }], 
    	numeric = [{ name: 'ONE', value: '1' }, { name: 'TWO', value: '2' }, { name: 'THREE', value: '3' }], 
    	symbol = [{ name: 'AT', value: '@' }, { name: 'HASH', value: '#' }, { name: 'DOLLAR', value: '$' }];
    
    const result = alpha.map(a => {
      a.children = numeric.map(n => {
        n.children = symbol.map(s => {
          return { ...s, id: a.value + n.value + s.value };
        });
        return { ...n, id: a.value + n.value };
      });
      return { ...a, id: a.value };
    })
    
    
    console.log(result[0].children[0].children[0].id)   // should be "a1@"
    console.log(result[1].children[0].children[0].id)   // should be "b1@"
    console.log(result[2].children[0].children[0].id)   // should be "c1@"
        2
  •  1
  •   Mark    7 年前

    你在数组上映射 symbols 很多次,但每次获取对相同对象的引用时:

    symbol.map(s => {  // s is always a reference to the same set of objects object
       s.id = a.value + n.value + s.value;
       return s;
    });
    

    每次看到这些对象之一时,都会覆盖 id 属性对 同一对象 ,所以当然,当您记录它们时,它们具有相同的ID。

    const alpha = [{ name: 'AAA', value: 'a' }, { name: 'BBB', value: 'b' }, { name: 'CCC', value: 'c' }], 
    	numeric = [{ name: 'ONE', value: '1' }, { name: 'TWO', value: '2' }, { name: 'THREE', value: '3' }], 
    	symbol = [{ name: 'AT', value: '@' }, { name: 'HASH', value: '#' }, { name: 'DOLLAR', value: '$' }];
    
    const result = alpha.map(a => {
      a.children = numeric.map(n => {
        n.children = symbol.map(s => {
          s.id = a.value + n.value + s.value;
          return s;
        });
        n.id = a.value + n.value;
        return n;
      });
      a.id = a.value;
      return a;
    })
    
    // these are the same objects:
    console.log("0 & 1 Same reference?", result[0].children[0].children[0] === result[1].children[0].children[0])
    
    console.log("1 & 2 Same reference?", result[1].children[0].children[0] === result[2].children[0].children[0])