Combining objects in an array with similar values

KontrollFreek

I've been searching all over the internet for an answer, including picking apart some code that does it correctly, but I can't seem to figure it out.

Basically, I want to take an existing array of objects

rank_data = [
    { name: "IRJBoss", point: 100 },
    { name: "RegainedORU", point: 100 },
    { name: "Kirbycube", point: 100 },
    { name: "RegainedORU", point: 96.735},
    { name: "Kirbycube", point: 96.735 },
    { name: "HanoCrux", point: 93.674 },
    { name: "Rynoxious", point: 93.674 },
    { name: "KanadianKiD", point: 93.674 },
    { name: "RegainedORU", point: 93.674 },
    { name: "LightShade", point: 93.674 },
    ...
]

And format them so all the points are added together, and each name is only used once

rank_data = [
    { name: "RegainedORU", point: 290.409 },
    { name: "Kirbycube", point: 196.735 },
    { name: "IRJBoss", point: 100 },
    { name: "HanoCrux", point: 93.674 },
    { name: "Rynoxious", point: 93.674 },
    { name: "KanadianKiD", point: 93.674 },
    { name: "LightShade", point: 93.674 },
    ...
]

What's the simplest way I could implement this into my code?

Nenad Vracar

Basically you an just group them by name using reduce and then sort by point.

const rank_data = [{"name":"IRJBoss","point":100},{"name":"RegainedORU","point":100},{"name":"Kirbycube","point":100},{"name":"RegainedORU","point":96.735},{"name":"Kirbycube","point":96.735},{"name":"HanoCrux","point":93.674},{"name":"Rynoxious","point":93.674},{"name":"KanadianKiD","point":93.674},{"name":"RegainedORU","point":93.674},{"name":"LightShade","point":93.674}]

const result = Object.values(rank_data.reduce((r, { name, point }) => {
  if (!r[name]) r[name] = { name, point }
  else r[name].point += point
  return r
}, {})).sort((a, b) => b.point - a.point)

console.log(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining objects in array based on key values

Combining objects with duplicate values inside an array

Combining objects in an array by common keys and values

Combining similar elements of an Array

Combining values of json objects into an array with unique object keys and its values

Combining array of objects with common values and return a key and value for each

Flatten and combining an Array of Objects

JavaScript - "Combining" two similar arrays of objects

Return similar values ​from multiple array of objects in Javascript

Array Combining Values

combining array values in looping

Excel: Combining columns with similar values into one

Combining values from Similar Strings in CSV File

Search array for similar objects

Jolt transformation combining multiple values in an array of objects when multiple objects are in input

how to perform sum/minus operation on an array of objects, based on similar values(not properties) in an array of objects

combining similar key/value pair into an array

AngularJS combining objects in array on properties

Swift - Combining an array of objects by an attribute

Group similar values in the array

Convert a javascript array of multiple similar objects into a smaller array with repeated values as a sub array

combining duplicate values in JavaScript array

Combine an array of values inside array of similar objects which has matching key value pair using JS

Querying MongoDB array of similar objects

Pairing Similar objects of an array in AngularJS

Merge objects in array with similar key

Fast Enumeration on Array with similar Objects

Deduping an array of objects with similar datetimes

Combining values in nested objects with common properties