2

I have an array that looks like this

var data = [
 {"target": "production", "datapoints": [[165.0, 136], [141.0, 176], [null, 176]]},
 {"target": "test", "datapoints": [[169.0, 100], [151.0, 160], [null, 120]]},
 {"target": "backup", "datapoints": [[1.0, 130], [32.0, 120], [13.0, 130]]}
]

Is it possible to search this array to return the hash that contains for example "production"

{"target": "production", "datapoints": [[165.0, 136], [141.0, 176], [null, 176]]}

I looked at this http://api.jquery.com/jQuery.inArray/ but didn't know how to use it.

5 Answers5

6

You can do:

var target = "production";
var result = $.grep(data, function(e){ return e.target == target; });

console.log(result);
putvande
  • 15,068
  • 3
  • 34
  • 50
  • So apparently a native for loop is faster than jquery ... I decided to switch to a native for loop ... what do you think? –  Aug 15 '13 at 20:02
3

Not sure if this is what you meant, but try this:

for(var i=0;i<data.length;i++) 
    if(data[i].target=='production') 
        return data[i]
        // or do something else/more

This will iterate over the array and check the 'target' attribute for the value 'production'

d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
3

You've mentioned associative arrays, so be it

In case you're doing a one time read (later modifications are allowed) and many time search over this data, it is much more sufficient to actually translate your array to Javascript object (becoming an associative array):

var assocArray = {};
for(var i = 0; i < data.length; i++)
{
    assocArray[data[i].target] = data[i];
}

and then searching for

assocArray[name]

would have O(1) complexity while looping over the array each time has O(n/2) => O(n). This would significantly speed up searches over this data. But this only makes sense when data is read once and searched many times. New hashes/targets can of course be added later or existing ones changed as well.

So in your case you would then simply get your production by

assocArray["production"]

and you'd get the whole object associated with this hash/target.

Note: This only works if you have unique hash/target names. Whenever you have several objects with the same hash/target the last one would be returned. Following is a solution when you have several objects in your array that have the same target/hash.

Multiple results per hash/target

In case you do have several objects, that have the same hash/target, your array to object conversion should be changed a bit.

var assocArray = {};
for(var i = 0; i < data.length; i++)
{
    // doesn't exist? create array
    if (!assocArray[data[i].target])
    {
        assocArray[data[i].target] = [];
    }

    // push an element
    assocArray[data[i].target].push(data[i]);
}

This will make an associative array (Javascript object) that consistently returns an array of elements with one or more objects. It makes later manipulation easier because in either case an Array is being returned. But it could easily be changed that on a single result it should just return an object an not an array with a single element.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0
function findHash(hash, data) {
  var dataLen = data.length;
  for(dataLen > 0; dataLen--;) {
    if( data[dataLen].target == hash) {
      return data[dataLen];
    }
  }
  return false;
}

I don't recommend using this on large arrays but it should work for you or get you on the right track.

Jeffpowrs
  • 4,430
  • 4
  • 30
  • 49
0

Try this function:

var getDataByTarget = function (target) {
    var ret = false;
    $.each(data, function () {
        if (this.target == target) {
            ret = this;
            return false;
        }
    });
    return ret;
};

Fiddle: http://jsfiddle.net/vUEYA/

Nicklas Nygren
  • 2,595
  • 13
  • 19