0

I have an array of objects, something like this example:

var b = [
  {
    'super_attribute[170]': "41", 
    'super_attribute[171]': "15",
    'data': 1
  },
  {
    'super_attribute[150]': "401", 
    'super_attribute[181]': "5",
    'test': 1234
  }
];

I want to select the object out of the array that has the attribute and value values from a

var a = {
  'super_attribute[170]': "41", 
  'super_attribute[171]': "15"
};

Is this possible with array filters or mapping?

Callum
  • 1,136
  • 3
  • 17
  • 43
  • Possible duplicate of [Search multi-dimensional array JavaScript](http://stackoverflow.com/questions/8809425/search-multi-dimensional-array-javascript) – Martin Apr 11 '17 at 13:49

1 Answers1

2
var filtered = b.filter(function(item){

return item.attribute == 'something' && item.value == 1;

});

edit: here you'll find the documentation to filter

satnam
  • 10,719
  • 5
  • 32
  • 42
Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
  • I just edited my original question with the updated code that I should have posted in the first place. The issue is that I the values in the object `a` will change – Callum Apr 11 '17 at 14:29
  • if the values in a change then don't compare like @Manish suggested with static values - rather compare with the right key from a: item.attribute == a.attribute ... ;) – MarcelD Apr 11 '17 at 18:01