1

I did a quick search for my question in the forms and this post basically covers it all apart from one aspect. I am just looking some advice on the proposal of a solution.

Basically, I have a list of name-value pairs in an array of objects

[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}] etc etc

Say we have 100 of these and I want to search for 1, one way to do this (the accepted answer in the linked post) would be as below:

var data = [{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}];
for(var i=0;i<data.length;i++){
  if(data[i]['name'] == 'name2'){
    console.log('The value is: ' + data[i]['value']);
    break;
  }
}

My question is, what if I want to find two values? Is there a more efficient way to do this other than looping through the array again looking for the second value? Say for example we were wanting to search for name1 AND name2. I was thinking of something along the lines of:

var data = [{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}];

for(var i=0;i<data.length;i++){
  var x = 0;
  if(data[i]['name'] == 'name1'  || data[i]['name'] == 'name2'){

    if (data[i]['name'] == 'name1'){
        console.log('The value for name 1 is: ' + data[i]['value']);
        x++
    }

    if (data[i]['name'] == 'name2'){
        console.log('The value for name 2 is: ' + data[i]['value']);
        x++
    }

    if (x == 2)
    {
        break;
    }

  }
}

This way we would only be looping through the array once and still breaking when both are found. Does this seem the best way to do it or would there be a more efficient solution?

Jones5672
  • 27
  • 5
  • 1
    There's nothing JSON related about this question: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/ – Quentin Jan 03 '18 at 10:50
  • 4
    That's an array of objects and not [JSON](http://json.org). To find (filter) specific elements in an array have a look at [`Array.prototype.filer()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Andreas Jan 03 '18 at 10:52
  • 1
    Possible duplicate of [Finding matching objects in an array of objects?](https://stackoverflow.com/questions/6237537/finding-matching-objects-in-an-array-of-objects) – Andreas Jan 03 '18 at 10:54

3 Answers3

1

I would use filter on the array to do it, it's a bit easier to read:

var data = [
  { name: 'name1', value: 'value1' },
  { name: 'name2', value: 'value2' }
];

var result = data.filter(function(item) {
  return item.name === 'name1' || item.name === 'name2';
});
Rick
  • 897
  • 6
  • 14
1

You can use a Map to look up values in O(1) time instead of using an Array in O(n) time after constructing it from the data in O(n) time:

var data = [{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}];

var map = new Map(data.map(function (obj) {
  return [obj.name, obj.value];
}));

console.log(map.get('name1'));
console.log(map.get('name2'));
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
0

For only one lookup, the values can be retrieved during the parsing:

var data = [], j = `[{"name":"name1","value":"value1"}, 
                 {"name":"name2","value":"value2"}, {"name":"name3","value":"value3"}]`

JSON.parse(j, function(key, value) { 
      if (value.name === 'name1' || value.name === 'name2') data.push(value); 
      return value; })

console.log( data )

For multiple lookups, it's more efficient to generate a lookup object:

var names={}, j = '[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]'

JSON.parse(j, function(key, value) { 
    if (value.name !== void 0) names[value.name] = value.value; 
    return value; })

console.log( names.name1, names["name2"] )
console.log( names )
Slai
  • 22,144
  • 5
  • 45
  • 53