sohal07
sohal07

Reputation: 482

Filter array dynamically based on other array in jQuery

I'm having trouble solving this situation where I need to filter an array based on the key values of another array. It might be quite easy but I can't catch the proper key value pairs.

I have this array of marks:

marks = {
  eng   : 90,
  maths : 50,
  phy   : 60,
  chem  : 75,
  bio   : 85,
}

And another array of tags with the same key names:

tags = {
  eng   : 'English',
  maths : 'Maths',
  phy   : 'Physics',
  chem  : 'Chemistry',
  bio   : 'Biology',
}

Now I need to filter tags array if the marks are >65 in marks array and the expected result is:

filteredTags = {
  eng   : 'English',
  chem  : 'Chemistry',
  bio   : 'Biology',
}

Or even better if it is:

filteredTags = ['English', 'Chemistry', 'Biology']

What I have tried so far:

filteredTags = []
$.each(marks, function(ind, val) {
  if (val > 60){
    filteredTags = tags.filter(function(i,v) {
      return (i == ind)
  }
})

Upvotes: 1

Views: 75

Answers (4)

Nina Scholz
Nina Scholz

Reputation: 386560

You could get the entries and filter the key/value pairs and get an array of tags.

var marks = { eng: 90, maths: 50, phy: 60, chem: 75, bio: 85 },
    tags = { eng: 'English', maths: 'Maths', phy: 'Physics', chem: 'Chemistry', bio: 'Biology' }
    result = Object
        .entries(marks)
        .filter(([_, v]) => v > 65)
        .map(([k]) => tags[k])
    
console.log(result);

Upvotes: 0

Swapnil S.
Swapnil S.

Reputation: 13

var marks = {
  eng   : 90,
  maths : 50,
  phy   : 60,
  chem  : 75,
  bio   : 85,
}
var tags = {
  eng   : 'English',
  maths : 'Maths',
  phy   : 'Physics',
  chem  : 'Chemistry',
  bio   : 'Biology',
}
var keysSorted = Object.keys(marks).sort(function(a,b){return marks[b]-marks[a]})

var finalResult = [];
for(var key in keysSorted)
    finalResult.push(tags[keysSorted[key]]); 
console.log(finalResult);

Upvotes: 0

MauriceNino
MauriceNino

Reputation: 6747

Would be easier with arrays, but this code should work:

let marks = {
  eng   : 90,
  maths : 50,
  phy   : 60,
  chem  : 75,
  bio   : 85,
}

let tags = {
  eng   : 'English',
  maths : 'Maths',
  phy   : 'Physics',
  chem  : 'Chemistry',
  bio   : 'Biology',
}

function getTagsWithMarksAbove(num) {
  let result = [];
  
  for(prop in marks) { // Iterate over the properties of the marks object
    if(marks[prop] > num) // Check if mark is above the specified number
      result.push(tags[prop]); // Add value of tags property with the same name to result array
  }
  
  return result;
}
console.log(getTagsWithMarksAbove(65))

Upvotes: 2

Taki
Taki

Reputation: 17654

You can reduce the array of Object.entries of the marks :

const marks = {
  eng: 90,
  maths: 50,
  phy: 60,
  chem: 75,
  bio: 85
};

const tags = {
  eng: "English",
  maths: "Maths",
  phy: "Physics",
  chem: "Chemistry",
  bio: "Biology"
};

const result = Object.entries(marks).reduce((all, [tag, score]) => {
  if (score > 65) all[tag] = tags[tag];
  return all;
}, {});

console.log(result); // Object 
console.log(Object.values(result)) // Array of values

Upvotes: 1

Related Questions