3

I have an array:

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

I want to remove the duplicates which has the same price, and only keep the last duplicate one then sort the array based on price from highest to lowest. Here is the result I want:

var result = [
  {price: 10, amount: 20},
  {price : 7, amount: 15},
  {price: 5, amount: 100},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]
1
  • 1
    but there are no duplicated ones on your result... Commented Mar 9, 2018 at 8:24

3 Answers 3

6

Use reduce to convert it an object first to remove the duplicates and last duplicate should override the previous one

var obj = arr.reduce( ( acc, c ) =>  Object.assign(acc, {[c.price]:c.amount}) , {});

Convert it back to array and sort the same

var output = Object.keys( obj )
              .map( s => ({ price : s, amount : obj[ s ] }) )
              .sort( ( a, b )  => b.price - a.price );

Demo

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
];
var obj = arr.reduce( ( acc, c ) =>  Object.assign(acc, {[c.price]:c.amount}) , {});
var output = Object.keys( obj )
              .map( s => ({ price : s, amount : obj[ s ] }) )
              .sort( ( a, b )  => b.price - a.price );
console.log( output );

Sign up to request clarification or add additional context in comments.

3 Comments

This seems very inefficient to me as it creates a lot of unnecessary intermediate objects and calls, and needs to know the entire structure of the input objects rather than just the price property. Using reduceRight, duplicates can be spliced from the original array, only creating a single additional object as an index of prices. Then the reduced array can be sorted.
@RobG So, to improve efficiency you decided to mutate the array?
The OP says "…I want to remove the duplicates" not "I want to replicate the array and all it's values (objects), then build another array filtered for certain values", which is what this does. ;-)
4

I'd use reduceRight and splice to remove duplicates. It doesn't create any useless intermediate objects, just a list of unique prices found along the way:

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

arr.reduceRight((acc, obj, i) => {
  acc[obj.price]? arr.splice(i, 1) : acc[obj.price] = true;
  return acc;
}, Object.create(null));

arr.sort((a, b) => b.price - a.price);

console.log(arr)

Comments

3

You can use Array.reduce to aggregate results in an array:

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

var results = arr.reduce<{ [price: string] : typeof arr[0] }>((p, e)=> {
    p[e.price] = e
    return p;
}, {});

var resultsAsArray = Object.keys(results)
     .map(k=>results[k])
     .sort((a, b) => b.price - a.price);

You can replace typeof arr[0] with the type of the array items if one is defined.

The idea of the solution is to acumulate the result in an object where the price is the key, if the same key is encountered multiple times, the old value is overwritten to in the end you will have only the last value for a given price.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.