更新属性并从JavaScript

时间:2019-05-30 11:57:10

标签: javascript arrays algorithm object ecmascript-6

我对某些代码有疑问。 这是我的数组

[
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 1,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 2,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 1,
    numberHoursSpent: 3,
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 4,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 3,
    numberHoursSpent: 5,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  }
  ]

问题是我需要使数组看起来像这样。

[
  {
    name: "Sale Network 1",
    numberSoldLodging: 3,
    numberHoursSpent: (1+2+4)/(1+1+1),
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 2,
    numberHoursSpent: (3+5)/(1+3),
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },

  ]
等效于

[
  {
    name: "Sale Network 1",
    numberSoldLodging: 3,
    numberHoursSpent: 2.33,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 2,
    numberHoursSpent: 2,
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },

  ]

首先,如果它们具有相同的“名称”属性,则在数组中找到相同的对象。然后,对所有numberHoursSpent属性求和,然后用这些对象的numberSoldLodgings属性之和除以它们。 然后,我删除所有相同的元素,只保留一个唯一的元素。 P / S:如果对象具有相同的“名称”属性,则它们是相同的。 这是我尝试过的。

handleData = arr => {
  var firstElementIndex =0;
  const final = []
  var filteredArray = []
  arr.map((e,i)=>{
    if(final.includes(e.name)){
      var k = k+arr[i].numberHoursSpent;
      firstElementIndex = final.indexOf(e.name);
      arr[firstElementIndex].numberHoursSpent +=  arr[i].numberHoursSpent
      filteredArray= arr.filter(item => item !== arr[i] )
    }
    else{
      final.push(e.name)
    }
    if(i == arr.length-1){
      arr[firstElementIndex].numberHoursSpent /= k
    }
  })

  return filteredArray;    
}

请帮助我,谢谢。

4 个答案:

答案 0 :(得分:2)

reducemap用于解构,扩展和Object.values

const data = [{name:"Sale Network 1",numberSoldLodging:1,numberHoursSpent:1,mail:"lanlehoang10@gmail.com",id:"5ceb9b5aff2d5732b4282886",created:1558944602810,updated:1558944602810},{name:"Sale Network 1",numberSoldLodging:1,numberHoursSpent:2,mail:"lanlehoang10@gmail.com",id:"5ceb9b5aff2d5732b4282886",created:1558944602810,updated:1558944602810},{name:"Sale Network 2",numberSoldLodging:1,numberHoursSpent:3,mail:"sale2@gmail.com",id:"5cef5a62b9c3113764e2183b",created:1559190114548,updated:1559190114548},{name:"Sale Network 1",numberSoldLodging:1,numberHoursSpent:4,mail:"lanlehoang10@gmail.com",id:"5ceb9b5aff2d5732b4282886",created:1558944602810,updated:1558944602810},{name:"Sale Network 2",numberSoldLodging:3,numberHoursSpent:5,mail:"lanlehoang10@gmail.com",id:"5ceb9b5aff2d5732b4282886",created:1558944602810,updated:1558944602810}];
const res = Object.values(data.reduce((acc, { name, numberSoldLodging, numberHoursSpent, ...rest }) => {
  acc[name] = acc[name] || { name, numberSoldLodging: 0, numberHoursSpent: 0, ...rest };
  acc[name].numberSoldLodging += numberSoldLodging;
  acc[name].numberHoursSpent += numberHoursSpent;
  return acc;
}, {})).map(({ numberSoldLodging, numberHoursSpent, ...rest }) => ({ numberSoldLodging, numberHoursSpent: numberHoursSpent / numberSoldLodging, ...rest }));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

请注意,reduce函数的第一行完成了重复的工作,因此您无需在末尾再调用filter

答案 1 :(得分:1)

function foo(arr) {
  let dict = {}
  arr.forEach(x => {
    if (dict[x.name]) {
      let y = dict[x.name]
      dict[x.name]['numberHoursSpent'] = y['numberHoursSpent'] + x['numberHoursSpent']
      dict[x.name]['numberSoldLodging'] = y['numberSoldLodging'] + x['numberSoldLodging']
    } else {
      dict[x.name] = x
    }
  })
  return Object.keys(dict).map(x => {
    dict[x]['numberHoursSpent'] = dict[x]['numberHoursSpent'] / dict[x]['numberSoldLodging']
    return dict[x]
  })
  // return dict
}

let arr = [{
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 1,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 2,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 1,
    numberHoursSpent: 3,
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 4,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 3,
    numberHoursSpent: 5,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  }
]

let y = foo(arr)
console.log(y)

答案 2 :(得分:0)

您可以尝试使用

let array = [{
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 1,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 2,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 1,
    numberHoursSpent: 3,
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 4,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 3,
    numberHoursSpent: 5,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  }
];


function filteredArray(array){
  let filteredArr = [];
  // Initialize the array
  filteredArr.push(array[0]);
  
  // Filtered the array
  for(let i=1; i<array.length; i++){
    let elementFound = false;
    
    for(let j=0; j<filteredArr.length; j++){
      if(array[i].name === filteredArr[j].name){ 
        elementFound = true;
        filteredArr[j].numberSoldLodging += array[i].numberSoldLodging;
        filteredArr[j].numberHoursSpent += array[i].numberHoursSpent;
      }
    }
    
    if(!elementFound)
      filteredArr.push(array[i]);
  }
  
  // Modified the 'numberHoursSpent'
  filteredArr.forEach(el => {
      el.numberHoursSpent /= el.numberSoldLodging;
      el.numberHoursSpent = parseFloat(el.numberHoursSpent.toFixed(2));
      });
  
  // Note : I have added toFixed for above long float integer '2.33333333333333333'
  
  return filteredArr;
}

console.log(filteredArray(array));

答案 3 :(得分:0)

尝试这个:

const qq = [
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 1,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 2,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 1,
    numberHoursSpent: 3,
    mail: "sale2@gmail.com",
    id: "5cef5a62b9c3113764e2183b",
    created: 1559190114548,
    updated: 1559190114548
  },
  {
    name: "Sale Network 1",
    numberSoldLodging: 1,
    numberHoursSpent: 4,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  },
  {
    name: "Sale Network 2",
    numberSoldLodging: 3,
    numberHoursSpent: 5,
    mail: "lanlehoang10@gmail.com",
    id: "5ceb9b5aff2d5732b4282886",
    created: 1558944602810,
    updated: 1558944602810
  }
  ];
  
  let p = new Map();
  for (let q of qq) {
    let s = p.get(q.name)
    if(s) {
      s.numberSoldLodging += q.numberSoldLodging;
      s.numberHoursSpent += q.numberHoursSpent;
      p.set(q.name, s);
    } else {
      p.set(q.name, q);
    }
    
  }
  
  

const result = [];
p.forEach((val, key, map) => {
  val.numberHoursSpent = val.numberHoursSpent / val.numberSoldLodging;
  result.push(val)
});

console.log(result);