TSR
TSR

Reputation: 20597

Efficient way to filter an object by key value

I need to create a candle value of an object with sorted keys:

 var obj = [{1: 'a'}, {2:'b'}, {3:'c'}, {4:'d'}, {5:'e'},{6: 'a'}, {7:'b'},{8:'c'}, {9:'d'}, {10:'e'}]

What is the most efficient way to filter this object based on the key?

For instance, I want: the obj filtered with 2 < keys <5 or 6 < keys < 9

(in reality, the keys are timestamp and the values are price data)

EDIT: Here is the object. That is what I meant by sorted

 var obj = [{1: 'a'}, {2:'b'}, {3:'c'}, {4:'d'}, {5:'e'},{6: 'a'}, {7:'b'},{8:'c'}, {9:'d'}, {10:'e'}]

Upvotes: 2

Views: 1497

Answers (6)

Emeeus
Emeeus

Reputation: 5260

As I see, you have an array not an object. Using filter and Object.keys and a condition like ((2<key && key<5) || (6<key && key< 9))

var arr = [{1: 'a'}, {2:'b'}, {3:'c'}, {4:'d'}, {5:'e'},{6: 'a'}, {7:'b'},{8:'c'}, {9:'d'}, {10:'e'}]

var newArr = arr.filter((o=>{
    let k = Object.keys(o);
    if((2<k[0] && k[0]<5) || (6<k[0] && k[0]< 9)){
        return true;
    }
}))

console.log(newArr)

Upvotes: 0

Chris Li
Chris Li

Reputation: 2671

You can use Object.keys to get the keys and reduce to create a new object with kv pair that met the condition

var obj = {1: 'a', 2:'b', 3:'c', 4:'d', 5:'e',6: 'a', 7:'b',8:'c', 9:'d', 10:'e'}
var res = Object.keys(obj).reduce((acc, elem) => {
	if (elem > 2 && elem < 5) acc[elem] = obj[elem]
	return acc
}, {})
console.log(res)

looks like the obj has been changed
you can filter the new obj a simple .filter()

var res = obj.filter(elem => Object.keys(elem)[0] > 2 && Object.keys(elem)[0] < 5)

if you decided to use the structure in comment

var obj = [{time: 1, value: 'a'},{time: 2, value: 'b'},{time: 3, value: 'c'},{time: 4, value: 'd'}]
var res = obj.filter(elem => elem.time > 2 && elem.time < 5)

Upvotes: 4

Amit Wagner
Amit Wagner

Reputation: 3264

what you need is to sort first the keys and then create new object with the filter and sorted values

i changed it to Map as object keys are not guaranteed to be sorted

const obj =  {1: 'a', 2:'b', 4:'c', 3:'d', 5:'e',6: 'a', 7:'b',8:'c', 9:'d', 10:'e'};

const newObj = new Map();
Object.keys(obj).filter(key =>{

    return (key > 2 && key < 5)
    }).sort().forEach(key=>{
      newObj.set(key,obj[key])


    })

Upvotes: 0

Chris Aby Antony
Chris Aby Antony

Reputation: 813

If you are using lodash it is as simple as follows:

const _ = require('lodash');
var obj = {1: 'a', 2:'b', 3:'c', 4:'d', 5:'e',6: 'a', 7:'b',8:'c', 9:'d', 10:'e'};
filtered = _.pickBy(obj,(val,key) =>{return ((key > 2) && (key < 5))})

Upvotes: 0

epascarello
epascarello

Reputation: 207537

Simple Objet.keys and reduce to create the new Object with the limited set of keys.

var obj = {1: 'a', 2:'b', 3:'c', 4:'d', 5:'e',6: 'a', 7:'b',8:'c', 9:'d', 10:'e'}

var filtered = Object.keys(obj).reduce((o, k) => {
  const num = Number(k)
  if (num > 4 && num < 9) {
   o[k] = obj[k]
  }
  return o
}, {})

console.log(filtered)

And just a warning that the object keys may not be in order of the numbers. So when you are outputting them, it is not guaranteed that 5 will be before 6.

Upvotes: 0

Eriks Klotins
Eriks Klotins

Reputation: 4180

like this?

keys() will give you an array of object keys, that you can filter

var obj = {1: 'a', 2:'b', 3:'c', 4:'d', 5:'e',6: 'a', 7:'b',8:'c', 9:'d', 10:'e'};
keys(obj).filter((a)=> parseInt(a) > 2 && parseInt(a) <5 );

Upvotes: -1

Related Questions