0

I am trying to sort a Multi-dimensional array returned from my API to allow people to choose a range based on beats.

To be honest i really stuck my api returns.

var myObj = [{
    title: 'title one',
    beats: 1
}, {
    title: 'title two',
    beats: 2
}, {
    title: 'title three',
    beats: 3
}, {
    title: 'title four',
    beats: 4
}, {
    title: 'title five',
    beats: 5
}, {
    title: 'title six',
    beats: 6
}, {
    title: 'title seven',
    beats: 7
}, {
    title: 'title eight',
    beats: 8
}, {
    title: 'title nine',
    beats: 9
}, {
    title: 'title ten',
    beats: 10
}];

Now i am trying to allow users to select a range based on beats.

so if they select 1-4 it would return.

var myObj = [{
    title: 'title one',
    beats: 1
}, {
    title: 'title two',
    beats: 2
}, {
    title: 'title three',
    beats: 3
}];

and 8-10 would return etc etc...

var myObj = [{
    title: 'title eight',
    beats: 8
}, {
    title: 'title nine',
    beats: 9
}, {
    title: 'title ten',
    beats: 10
}];

What function would i use to do this would appreciate any help on this?

user1503606
  • 3,872
  • 13
  • 44
  • 78

3 Answers3

1

@qubyte's answer to How to get all properties values of a Javascript Object (without knowing the keys)?

tells us how to enumerate over all the values of a returned Object.

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        var val = obj[key];
        // use val
    }
}

In your example, each value in the returned myObj is itself an object with properties "title" and "beats", and you want to search the entire myObj for those that have particular beats.

Let's start by making a function that searches over properties of the values, and returns an array having the desired values.

function searchByProperty(obj, property, low, high){
  var found = [];
  var val, prop;
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        val = obj[key];
        prop = val[property];
        if( (prop>=low) && (prop<=high) ) found.push(val);
    }
  return found;
}

Now we can use it like this:

   searchByProperty(myObj, 'beats', 1, 4)

will return:

[
    {
        title: 'title one',
        beats: 1
    },
    {
        title: 'title two',
        beats: 2
    },
    {
        title: 'title three',
        beats: 3
    },
    {
        title: 'title four',
        beats: 4
    }
]
Community
  • 1
  • 1
Paul
  • 26,170
  • 12
  • 85
  • 119
  • This is actually the answer to the first version of your question. If the myObj is an Array instead of an Object, then the loop in `searchByProprerty` can be changed to `for(var i=0,l=myArray.length; i – Paul Sep 22 '14 at 17:14
1

Since you have made myObj a real array, you can do (very short and very fast):

function getBeatsArray(myArray, low, high) {
    return myArray.filter(function(b) {
        return b.beats <= high && b.beats >=low;
    });
}
Martin Ernst
  • 3,199
  • 1
  • 12
  • 12
0

I made a function which works the way I think you want it

function getBeatsObj(allBeatsObj,rangeStart,rangeEnd) {
    var returnObj = {};
    for(var i = rangeStart; i <= rangeEnd; i++) {
        returnObj[i - 1] = allBeatsObj[i - 1];
    }
    return returnObj;
}
Mandera
  • 2,647
  • 3
  • 21
  • 26
  • This won't work if the relationship between the returned Object's keys and the beats property of the values is only a lucky coincidence. – Paul Sep 22 '14 at 16:53