使用键和值数组将对象的javascript数组转换为对象

使用键和值数组将对象的javascript数组转换为对象,javascript,arrays,algorithm,object,javascript-objects,Javascript,Arrays,Algorithm,Object,Javascript Objects,我有一组需要转换成新对象的对象。一些值作为键和值数组 const data = [ { type: 'user', id: 1 }, { type: 'user', id: 2 }, { type: 'group', id: 1 }, { type: 'group', id: 2 }, { type: 'user', id: 3 }, { type: 'user',

我有一组需要转换成新对象的对象。一些值作为键和值数组

const data = [
  {
    type: 'user',
    id: 1
  },
  {
    type: 'user',
    id: 2
  },
  {
    type: 'group',
    id: 1
  },
  {
    type: 'group',
    id: 2
  },
  {
    type: 'user',
    id: 3
  },
  {
    type: 'user',
    id: 4
  }
]
期望的结果是

const result = {
  user: [1, 2, 3, 4],
  group: [1, 2]
}
我尝试使用的
减少了
,但这不是我所期望的

const result = data.reduce((acc, { type, ...obj }) => {
  acc[type] = data.map(item => item.id)
  return acc;
}, {})

result = { user: [ 1, 2, 1, 2, 3, 4 ], group: [ 1, 2, 1, 2, 3, 4 ] }

您需要检查该属性是否不存在,并将
id
推送到该属性

const
data=[{type:'user',id:1},{type:'user',id:2},{type:'group',id:1},{type:'group',id:2},{type:'user',id:3},{type:'user',id:4}],
结果=数据.reduce((acc,{type,id})=>{
acc[类型]??=[];
acc[类型]。推送(id);
返回acc;
}, {});

控制台日志(结果)
您需要检查该属性是否不存在,并将
id
推送到该属性

const
data=[{type:'user',id:1},{type:'user',id:2},{type:'group',id:1},{type:'group',id:2},{type:'user',id:3},{type:'user',id:4}],
结果=数据.reduce((acc,{type,id})=>{
acc[类型]??=[];
acc[类型]。推送(id);
返回acc;
}, {});

控制台日志(结果)如果累加器上不存在该属性,则添加它,如果它确实将
id
推入该属性:

const data=[{“type”:“user”,“id”:1},{“type”:“user”,“id”:2},{“type”:“group”,“id”:1},{“type”:“group”,“id”:2},{“type”:“user”,“id”:3},{“type”:“user”,“id”:4}];
const result=data.reduce((acc,{type,id})=>{
如果(!acc[type])acc[type]=[];
acc[类型]。推送(id);
返回acc;
}, {})

console.log(result)
如果累加器上不存在该属性,则添加它,如果它确实将
id
推入其中:

const data=[{“type”:“user”,“id”:1},{“type”:“user”,“id”:2},{“type”:“group”,“id”:1},{“type”:“group”,“id”:2},{“type”:“user”,“id”:3},{“type”:“user”,“id”:4}];
const result=data.reduce((acc,{type,id})=>{
如果(!acc[type])acc[type]=[];
acc[类型]。推送(id);
返回acc;
}, {})

console.log(result)
除非您使用预定义的reducer函数进行函数编程,
reduce
通常是一个过于复杂的工具。只需使用一个循环

在这种情况下,您可以从具有空数组的
user
group
属性的对象开始,然后使用动态属性名称添加到正确的数组中:

const result = { user: [], group: [] };
for (const {type, id} of data) {
    result[type].push(id);
}
实例:

const数据=[
{
键入:“用户”,
身份证号码:1
},
{
键入:“用户”,
身份证号码:2
},
{
键入:“组”,
身份证号码:1
},
{
键入:“组”,
身份证号码:2
},
{
键入:“用户”,
身份证号码:3
},
{
键入:“用户”,
身份证号码:4
}
];
const result={user:[],group:[]};
for(数据的常量{type,id}){
结果[类型]。推送(id);
}

控制台日志(结果)除非您使用预定义的reducer函数进行函数编程,
reduce
通常是一个过于复杂的工具。只需使用一个循环

在这种情况下,您可以从具有空数组的
user
group
属性的对象开始,然后使用动态属性名称添加到正确的数组中:

const result = { user: [], group: [] };
for (const {type, id} of data) {
    result[type].push(id);
}
实例:

const数据=[
{
键入:“用户”,
身份证号码:1
},
{
键入:“用户”,
身份证号码:2
},
{
键入:“组”,
身份证号码:1
},
{
键入:“组”,
身份证号码:2
},
{
键入:“用户”,
身份证号码:3
},
{
键入:“用户”,
身份证号码:4
}
];
const result={user:[],group:[]};
for(数据的常量{type,id}){
结果[类型]。推送(id);
}

控制台日志(结果)每次迭代,您都会检查累积对象是否具有该类型,如果是,则将迭代元素的id推送到现有数组,否则初始化该数组

const数据=[
{
类型:“用户”,
id:1,
},
{
类型:“用户”,
id:2,
},
{
类型:“组”,
id:1,
},
{
类型:“组”,
id:2,
},
{
类型:“用户”,
id:3,
},
{
类型:“用户”,
id:4,
},
]
const res=数据减少((acc,el)=>{
if(附件[el.type]){
附件[el.type].推送(el.id)
}否则{
附件[el.type]=[el.id]
}
返回acc
}, {})

log(res)
每次迭代,您都会检查累积对象是否具有该类型,如果是,则将迭代元素的id推送到现有数组,否则初始化该数组

const数据=[
{
类型:“用户”,
id:1,
},
{
类型:“用户”,
id:2,
},
{
类型:“组”,
id:1,
},
{
类型:“组”,
id:2,
},
{
类型:“用户”,
id:3,
},
{
类型:“用户”,
id:4,
},
]
const res=数据减少((acc,el)=>{
if(附件[el.type]){
附件[el.type].推送(el.id)
}否则{
附件[el.type]=[el.id]
}
返回acc
}, {})
console.log(res)
这个怎么样

groupBy()的普通版本

您可以使用

const result = groupBy(data, 'type');
完整代码:

const groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x.id);
    return rv;
  }, {});
};

const data = [
  {
    type: 'user',
    id: 1
  },
  {
    type: 'user',
    id: 2
  },
  {
    type: 'group',
    id: 1
  },
  {
    type: 'group',
    id: 2
  },
  {
    type: 'user',
    id: 3
  },
  {
    type: 'user',
    id: 4
  }
]

const result = groupBy(data, 'type');

console.log(result); // { group: [1, 2], user: [1, 2, 3, 4] }
参考资料。

这个怎么样

groupBy()的普通版本

您可以使用

const result = groupBy(data, 'type');
完整代码:

const groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x.id);
    return rv;
  }, {});
};

const data = [
  {
    type: 'user',
    id: 1
  },
  {
    type: 'user',
    id: 2
  },
  {
    type: 'group',
    id: 1
  },
  {
    type: 'group',
    id: 2
  },
  {
    type: 'user',
    id: 3
  },
  {
    type: 'user',
    id: 4
  }
]

const result = groupBy(data, 'type');

console.log(result); // { group: [1, 2], user: [1, 2, 3, 4] }

参考。

这里是一个使用
对象的解决方案。fromEntries
为结果对象构建框架(使用空数组)。然后,一个简单的循环就可以填充这些数组,您就完成了:

let data=[{type:'user',id:1},{type:'user',id:2},{type:'group',id:1},{type:'group',id:2},{type:'user',id:3},{type:'user',id:4}];
让result=Object.fromEntries(data.map({type})=>[type,[]);
对于(数据的{type,id})结果[type].push(id);

控制台日志(结果)这里是一个使用
对象的解决方案。fromEntries
为结果对象构建框架(使用空数组)。然后,一个简单的循环就可以填充这些数组,您就完成了:

let data=[{type:'user',id:1},{type:'user',id:2},{type:'group',id:1},{type:'group',id:2},{type:'user',id:3},{type:'user',id:4}];
让result=Object.fromEntries(data.map({type})=>[type,[]);
对于dat的(let{type,id})