简体   繁体   中英

Efficient name value pairs extraction from array of objects using JavaScript

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?

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';
});

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')); 

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 ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM