1

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

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

I want to select the object out of the array in b that contains the attributes of object a

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

Is this possible, maybe with jQuery.grep or mapping? (I am using jQuery.)

2
  • 3
    This has nothing to do with jQuery. It does have to do with JavaScript. I've retagged and updated for you. Commented Apr 12, 2017 at 9:13
  • You only want to match if all of a's properties match the entry from b, right? So there'd be no match if a's attribute[171] were "42" instead of "15" given the b shown... Commented Apr 12, 2017 at 9:27

1 Answer 1

5

You can use filter to loop through b, and loop through a's properties looking for matches on each entry in b. It can be useful to grab a list of a's properties in advance.

var aprops = Object.keys(a);
var c = b.filter(function(entry) {
  return aprops.every(function(key) {
    return entry[key] === a[key];
  });
});

var b = [
  {
    'attribute[170]': "41", 
    'attribute[171]': "15",
    'data': 1,
    'something': 'some text'
  },
  {
    'attribute[150]': "401", 
    'attribute[181]': "5",
    'test': '1234',
    'data': 2.3
  }
];
var a = {
  'attribute[170]': "41", 
  'attribute[171]': "15"
};

var aprops = Object.keys(a);
var c = b.filter(function(entry) {
  return aprops.every(function(key) {
    return entry[key] === a[key];
  });
});
console.log(c);

Or using ES2015+ syntax:

const aprops = Object.keys(a);
const c = b.filter(entry => aprops.every(key => entry[key] === a[key]));

const b = [
  {
    'attribute[170]': "41", 
    'attribute[171]': "15",
    'data': 1,
    'something': 'some text'
  },
  {
    'attribute[150]': "401", 
    'attribute[181]': "5",
    'test': '1234',
    'data': 2.3
  }
];
const a = {
  'attribute[170]': "41", 
  'attribute[171]': "15"
};

const aprops = Object.keys(a);
const c = b.filter(entry => aprops.every(key => entry[key] === a[key]));
console.log(c);

That gives you an array with all matching objects. If you only want the first matching object, not in an array, you'd use find (added in ES2015 aka ES6, but easily polyfilled/shimmed) instead of filter:

var aprops = Object.keys(a);
var c = b.find(function(entry) {
  return aprops.every(function(key) {
    return entry[key] === a[key];
  });
});

var b = [
  {
    'attribute[170]': "41", 
    'attribute[171]': "15",
    'data': 1,
    'something': 'some text'
  },
  {
    'attribute[150]': "401", 
    'attribute[181]': "5",
    'test': '1234',
    'data': 2.3
  }
];
var a = {
  'attribute[170]': "41", 
  'attribute[171]': "15"
};

var aprops = Object.keys(a);
var c = b.find(function(entry) {
  return aprops.every(function(key) {
    return entry[key] === a[key];
  });
});
console.log(c);

Or using ES2015+ syntax:

const aprops = Object.keys(a);
const c = b.find(entry => aprops.every(key => entry[key] === a[key]));

const b = [
  {
    'attribute[170]': "41", 
    'attribute[171]': "15",
    'data': 1,
    'something': 'some text'
  },
  {
    'attribute[150]': "401", 
    'attribute[181]': "5",
    'test': '1234',
    'data': 2.3
  }
];
const a = {
  'attribute[170]': "41", 
  'attribute[171]': "15"
};

const aprops = Object.keys(a);
const c = b.find(entry => aprops.every(key => entry[key] === a[key]));
console.log(c);

Sign up to request clarification or add additional context in comments.

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.