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

将对象数组转换为单个对象

  •  0
  • Sameer  · 技术社区  · 8 年前

    这是我的初始阵列:

    [ 
       { 
        id: 1,
        name: 'pos',
        code: 'pos123',
        appCode: 22,
       },
      { 
        id: 2,
        name: 'tailor',
        code: 'tailor123',
        appCode: 21,
      } 
    ]
    

    我想要的输出:

    {
       pos123: {
        id: 1,
        name: 'pos',
        code: 'pos123',
        appCode: 22,
      },
      tailor123: {
        id: 2,
        name: 'tailor',
        code: 'tailor123',
        appCode: 21,
      },
    }
    

    我试着用 map ,但我想不出来。我应该用吗 reduce 在这里

    6 回复  |  直到 8 年前
        1
  •  3
  •   CertainPerformance    8 年前

    你的输出不是一个数组,而是一个对象,所以你必须使用 reduce .你只能使用 map 将一个数组中的X个项目数转换为另一个数组中的X个项目数。

    const input=[{id:1,name:'pos',code:'pos123',appCode:22,},{id:2,name:'tailor',code:'tailor123',appCode:21,}]
    const output = input.reduce((a, obj) => {
      a[obj.code] = obj;
      return a;
    }, {});
    console.log(output);
        2
  •  1
  •   Dhaval Chaudhary    8 年前
    var yourArray = [ 
     { 
      id: 1,
      name: 'pos',
      code: 'pos123',
      appCode: 22,
      },
     {  
      id: 2,
      name: 'tailor',
      code: 'tailor123',
      appCode: 21,
     } 
    ];
    
    //this is what you required
    var output =  yourArray.reduce((obj, item) => {
      obj[code] =item;
      return obj;
    }, {});
    
        3
  •  1
  •   mickl    8 年前

    你可以用 Object.assign 将数组映射到键和值的预期形状。尝试:

    let array = [ 
       { 
        id: 1,
        name: 'pos',
        code: 'pos123',
        appCode: 22,
       },
      { 
        id: 2,
        name: 'tailor',
        code: 'tailor123',
        appCode: 21,
      } 
    ];
    
    let output = Object.assign(...array.map(x => ({ [x.code]: x })));
    
    console.log(output);
        4
  •  0
  •   brk    8 年前

    使用数组reduce函数

    var data = [{
        id: 1,
        name: 'pos',
        code: 'pos123',
        appCode: 22,
      },
      {
        id: 2,
        name: 'tailor',
        code: 'tailor123',
        appCode: 21,
      }
    ]
    
    var modData = data.reduce(function(acc, curr) {
      //acc is the empty array passed as argument
      // if empty array does not have a property pos123 or so 
      // create a key by this name
      if (!acc.hasOwnProperty(curr.code)) {
        // then add property to it
        acc[curr.code] = {
          id: curr.id,
          name: curr.name,
          appCode: curr.appCode
        }
      }
    
      return acc;
    
    }, {})
    
    console.log(modData)
        5
  •  0
  •   Matus Dubrava    8 年前

    使用loop可以做到这一点。

    const resp = [
       {
        id: 1,
        name: 'pos',
        code: 'pos123',
        appCode: 22,
       },
      {
        id: 2,
        name: 'tailor',
        code: 'tailor123',
        appCode: 21,
      }
    ]
    
    let res = {};
    
    for (let i = 0; i < resp.length; i++) {
      res[resp[i]['code']] = {
        id: resp[i]['id'],
        name: resp[i]['name'],
        appCode: resp[i]['appCode'],
        code: resp[i]['code']
      }
    }
    
    console.log(res);
        6
  •  0
  •   KooiInc    8 年前

    其他解决方案似乎包括“代码”——结果对象中的属性。这个不行

    const test = [ 
      { 
       id: 1,
       name: 'pos',
       code: 'pos123',
       appCode: 22,
      }, { 
       id: 2,
       name: 'tailor',
       code: 'tailor123',
       appCode: 21,
      } 
    ];
    const test2 = JSON.parse(JSON.stringify(test));
    const result = document.querySelector("#result");
    
    result.textContent = "**No redundancy\n" + JSON.stringify(
      test.reduce( (newObj, el) => 
        (newObj[el.code] = delete el.code && el) && newObj, {} 
      ), null, " ");
    
    // alternative
    result.textContent += "\n\n**Alternative\n" + JSON.stringify(
      test2.reduce( (newObj, el) => (newObj[el.code] = el) && newObj, {}
      ), null, " ");
    <pre id="result"></pre>