简体   繁体   English

将具有相同属性的多个对象转换为单个对象 object - Javascript

[英]Transform multiple objects with identic property into single one object - Javascript

I couldn't find a good example or any resources on how to actually transform multiple objects that contain the same property ( fullName ) in my case and how to make only 1 object with the following structure (in order to work on just 1 object instead of multiple)我找不到一个很好的例子或任何资源,说明如何在我的情况下实际转换包含相同属性( fullName )的多个对象,以及如何仅使用以下结构制作 1 个 object (以便仅使用 1 个 object多个)

The sample objects look like this:示例对象如下所示:

{
  fullName: 'Reuben Daly',
  from: '2015-06-01',
  to: '2016-07-01',
  companyName: 'Mondelēz International'
}
{
  fullName: 'Reuben Daly',
  from: '2017-04-01',
  to: '2017-12-01',
  companyName: 'Redgate Software'
}
{
  fullName: 'Reuben Daly',
  from: '2013-10-01',
  to: '2014-01-01',
  companyName: 'Cambridge International Examinations'
}
...

and the goal is to create 1 object that is actually containing all of the values stored in 1, like this:目标是创建 1 个 object,它实际上包含存储在 1 中的所有值,如下所示:

{
  fullName: 'Reuben Daly',
  pos1from: '2015-06-01',
  pos1to: '2016-07-01',
  pos1companyName: 'Mondelēz International',
  pos2from: '2017-04-01',
  pos2to: '2017-12-01',
  pos2companyName: 'Redgate Software'
  pos3from: '2013-10-01,
  pos3to: '2014-01-01',
  pos3companyName: 'Cambridge International Examinations'
  ...

}

I have been trying to achieve that with reduce() but as I'm having objects instead of arrays it won't work.我一直在尝试使用 reduce() 来实现这一点,但是由于我使用的是对象而不是 arrays,所以它不起作用。 Been reading about the forEach() method but can't find a good example that is actually doing something similar and I've stuck.一直在阅读 forEach() 方法,但找不到一个实际上在做类似事情的好例子,我一直坚持。

You should use an object as initial value in reduce:您应该使用 object 作为 reduce 的初始值:

const data=[
    {
  fullName: 'Reuben Daly',
  from: '2015-06-01',
  to: '2016-07-01',
  companyName: 'Mondelēz International'
},
{
  fullName: 'Reuben Daly',
  from: '2017-04-01',
  to: '2017-12-01',
  companyName: 'Redgate Software'
},
{
  fullName: 'Reuben Daly',
  from: '2013-10-01',
  to: '2014-01-01',
  companyName: 'Cambridge International Examinations'
},
]

const result =  data.reduce((acc,elem, index)=>({
        ...acc,
        fullName:elem.fullName,
        [`from${index+1}`]:elem.from,
        [`to${index+1}`]:elem.to,
        [`companyName${index+1}`]:elem.companyName
    }   ),{})

 const arr = [{ fullName: 'Reuben Daly', from: '2015-06-01', to: '2016-07-01', companyName: 'Mondelēz International' }, { fullName: 'Reuben Daly', from: '2017-04-01', to: '2017-12-01', companyName: 'Redgate Software' }, { fullName: 'Reuben Daly', from: '2013-10-01', to: '2014-01-01', companyName: 'Cambridge International Examinations' }]; const result = arr.reduce((acc, cur, i) => { if (i === 0) { acc = {fullName: cur.fullName}; } ["from", "to", "companyName"].forEach(attr => { acc[`pos${i + 1}${attr}`] = cur[attr]; }); return acc; }, {}); console.log(result);

First put all those objects into an array.首先将所有这些对象放入一个数组中。

const objArr = [o1,o2,o3]; etc. 

What you can do from here is iterate over the objects and then within that loop nest a second loop that will iterate over the keys of each object.您可以从这里做的是遍历对象,然后在该循环中嵌套第二个循环,该循环将遍历每个 object 的键。 You can modify the key, providing it isn't the fullName key or one whose value you know will not change, for the new object that will accumulate all the properties.您可以修改密钥,前提是它不是 fullName 密钥或您知道其值不会更改的密钥,因为新的 object 将累积所有属性。

const newObj = {o1.fullName};  
for(let i = 0; i < objArr.length; i++){
    let o = objArr[i]; 
    for(let key in o){
        if(key !== 'fullName'){
            let newKey = 'pos' + (i + 1) + key;
            newObj[newKey] = o[key]; 
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM