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?

3

3 Answers 3

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';
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to all who answered. I was happy to go with this answer.
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'));

Comments

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.