-1

i want to populate an array with the value of property name if the num matches with the property num of array of objects. i want to do this in ES5, how can i do? example arr['Luke', 'Martin']

var num = 1
arr = []
arrObj = [{
  name: 'Mark',
  num: 3
},
{
name: 'Paul',
 num: 4
 },
{
name: 'Luke',
 num: 1
 },
 {
 name: 'Martin',
 num: 1
 }
 ]
fabrizio
  • 11
  • 4
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – evolutionxbox Aug 06 '22 at 00:02

1 Answers1

0

You can use the forEach method to check the properties against the variable value.

var num = 1
arr = []
arrObj = [
 {
   name: 'Mark',
   num: 3
 },
 {
   name: 'Paul',
   num: 4
 },
 {
   name: 'Luke',
   num: 1
 },
 {
   name: 'Martin',
   num: 1
 }
 ]
 
 arrObj.forEach(i => {
  if (i.num === num) arr.push(i.name);
 });
 console.log(arr);
Sarah
  • 354
  • 1
  • 12