7

What is most efficient / elegant way to achieve sql-like filtering effect. I want to filter them and get only that objects which are max value in some group.

This is my code, it works but probably it's not best way:

uniqueValues = (arr) => [...new Set(arr)];
getMaxTimeOf = (arr) => Math.max(...arr.map(o => o.timeStamp), 0);
selectorName = (name) => (obj) => obj.name === name;
selectorTime = (time) => (obj) => obj.timeStamp === time;
getGroup = (obj, selector) => obj.filter(selector)

onlyLastChangedFrom = (history) => {
const uniqueNames = uniqueValues(history.map(o => o.name))
let filtered = []
 uniqueNames.forEach(name => {
  const group = getGroup(history, selectorName(name))
  const groupLastTime = getMaxTimeOf(group)
  const lastChange = getGroup(group, selectorTime(groupLastTime))
  filtered.push(lastChange[0])
 });
 return filtered
}   
onlyLastChangedFrom(history)
    // Input:
    [ { name: 'bathroom',
        value: 54,
        timeStamp: 1562318089713 },
      { name: 'bathroom',
        value: 55,
        timeStamp: 1562318090807 },
      { name: 'bedroom',
        value: 48,
        timeStamp: 1562318092084 },
      { name: 'bedroom',
        value: 49,
        timeStamp: 1562318092223 },
      { name: 'room',
        value: 41,
        timeStamp: 1562318093467 } ]

    // Output:
    [ { name: 'bathroom',
        value: 55,
        timeStamp: 1562318090807 },
      { name: 'bedroom',
        value: 49,
        timeStamp: 1562318092223 },
      { name: 'room',
        value: 41,
        timeStamp: 1562318093467 } ]
moraviec
  • 85
  • 1
  • 4

8 Answers8

13

Reduce the array to an object, using the name property as the key. For each item, check if the item that exists in the accumulator has a higher value than the current item, and if not replace it with the current item. Convert back to an array with Object.values():

const arr = [{"name":"bathroom","value":54,"timeStamp":1562318089713},{"name":"bathroom","value":55,"timeStamp":1562318090807},{"name":"bedroom","value":48,"timeStamp":1562318092084},{"name":"bedroom","value":49,"timeStamp":1562318092223},{"name":"room","value":41,"timeStamp":1562318093467}]

const result = Object.values(arr.reduce((r, o) => {
  r[o.name] = (r[o.name] && r[o.name].value > o.value) ? r[o.name] : o

  return r
}, {}))

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • 1
    No. We need to assign to `r[o.name]` the result of the ternary `r[o.name] && r[o.name].value > o.value ? r[o.name] : o`. – Ori Drori Nov 25 '20 at 10:43
  • 1
    I see then "r[o.name] = r[o.name] && r[o.name].value > o.value ? r[o.name] : o" is "r[o.name] = (r[o.name] && r[o.name].value > o.value) ? r[o.name] : o" – Cuban coffee Nov 25 '20 at 10:46
2

I love to use lodash for stuff like this. It's very functional and therefore very clear and straightforward.

Take a look at the following code:

const DATA = [
  {
    name: "bathroom",
    value: 54,
    timeStamp: 1562318089713
  },
  {
    name: "bathroom",
    value: 55,
    timeStamp: 1562318090807
  },
  {
    name: "bedroom",
    value: 48,
    timeStamp: 1562318092084
  },
  {
    name: "bedroom",
    value: 49,
    timeStamp: 1562318092223
  },
  {
    name: "room",
    value: 41,
    timeStamp: 1562318093467
  }
];

let max = _
  .chain(DATA)
  .groupBy('name')
  .sortBy('value')
  .map(o => _(o).reverse().first())
  .flatten()
  .value();

console.log(max); // returns [{"name":"bathroom","value":55,"timeStamp":1562318090807},{"name":"bedroom","value":49,"timeStamp":1562318092223},{"name":"room","value":41,"timeStamp":1562318093467}]
StefanN
  • 871
  • 6
  • 19
  • Chain lodash function is not friendly with lodash es (when you want use treeshaking) To use chain function with es import see that answer on github https://github.com/lodash/lodash/issues/3298#issuecomment-341685354 – rdhainaut Oct 01 '21 at 08:52
1

What is most efficient / elegant way to achieve sql-like filtering effect.

You could take functions for every step and pipe all functions for a single result.

For example in SQL, you would have the following query:

SELECT name, value, MAX(timeStamp) 
FROM data 
GROUP BY name;

With an SQL like approach, you could group first and take the max object out of the result sets.

result = pipe(
    groupBy('name'),
    select(max('timeStamp'))
)(data);

const
    pipe = (...functions) => input => functions.reduce((acc, fn) => fn(acc), input),
    groupBy = key => array => array.reduce((r, o) => {
        var temp = r.find(([p]) => o[key] === p[key])
        if (temp) temp.push(o);
        else r.push([o]);
        return r;
    }, []),
    max = key => array => array.reduce((a, b) => a[key] > b[key] ? a : b),
    select = fn => array => array.map(fn);


var data = [{ name: 'bathroom', value: 54, timeStamp: 1562318089713 }, { name: 'bathroom', value: 55, timeStamp: 1562318090807 }, { name: 'bedroom', value: 48, timeStamp: 1562318092084 }, { name: 'bedroom', value: 49, timeStamp: 1562318092223 }, { name: 'room', value: 41, timeStamp: 1562318093467 }],
    result = pipe(
        groupBy('name'),
        select(max('timeStamp'))
    )(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Here is another reduce alternative :

var arr = [{"name":"bathroom","value":54,"timeStamp":1562318089713},{"name":"bathroom","value":55,"timeStamp":1562318090807},{"name":"bedroom","value":48,"timeStamp":1562318092084},{"name":"bedroom","value":49,"timeStamp":1562318092223},{"name":"room","value":41,"timeStamp":1562318093467}];

var obj = arr.reduce((r, o) => (o.value < (r[o.name] || {}).value || (r[o.name] = o), r), {});

console.log( Object.values(obj) );
Slai
  • 22,144
  • 5
  • 45
  • 53
0

You can use .reduce() by keeping an accumulated object which keeps the max group currently found and then use Object.values() to get an array of those objects (instead of an key-value pair object relationship).

See example below:

const arr=[{name:"bathroom",value:54,timeStamp:1562318089713},{name:"bathroom",value:55,timeStamp:1562318090807},{name:"bedroom",value:48,timeStamp:1562318092084},{name:"bedroom",value:49,timeStamp:1562318092223},{name:"room",value:41,timeStamp:1562318093467}];

const res = Object.values(arr.reduce((acc, o) => {
  acc[o.name] = acc[o.name] || o;
  if (o.value > acc[o.name].value)
    acc[o.name] = o;
  return acc;
}, {}));

console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Doing this in stages.

  1. get a set of names
  2. create a sorted array in descending order
  3. then just map using find to get the first one

Below is an example.

const input = [{"name":"bathroom","value":54,"timeStamp":1562318089713},{"name":"bathroom","value":55,"timeStamp":1562318090807},{"name":"bedroom","value":48,"timeStamp":1562318092084},{"name":"bedroom","value":49,"timeStamp":1562318092223},{"name":"room","value":41,"timeStamp":1562318093467}];

const output = [...new Set(input.map(m => m.name))].
  map(m => [...input].sort(
    (a,b) => b.value - a.value).
    find(x => m === x.name));
  
console.log(output);
Keith
  • 22,005
  • 2
  • 27
  • 44
0

Use map() and foreach() to get desired output

const arr=[{name:"bathroom",value:54,timeStamp:1562318089713}, 
    {name:"bathroom",value:55,timeStamp:1562318090807}, 
    {name:"bedroom",value:48,timeStamp:1562318092084}, 
    {name:"bedroom",value:49,timeStamp:1562318092223}, 
    {name:"room",value:41,timeStamp:1562318093467}];

let res = new Map();
arr.forEach((obj) => {
    let values = res.get(obj.name);
    if(!(values && values.value > obj.value)){ 
        res.set(obj.name, obj) 
    }
})
console.log(res);
console.log([...res])
0

You can use sort and filter methods

const arr = [{"name":"bathroom","value":54,"timeStamp":1562318089713},{"name":"bathroom","value":55,"timeStamp":1562318090807},{"name":"bedroom","value":48,"timeStamp":1562318092084},{"name":"bedroom","value":49,"timeStamp":1562318092223},{"name":"room","value":41,"timeStamp":1562318093467}]
arr.sort(function (a, b) {
    return b.value - a.value;
}).filter((v, i, a) => a.findIndex((v2) => v2.name === v.name) === i);

console.log(arr);
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47