1

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)
  }
})
2
  • Your code keeps overwriting filteredTags. Commented Jul 3, 2019 at 14:41
  • const filteredTags = Object.keys(marks).filter(key => marks[key] > 65).map(key => tags[key]); Commented Jul 3, 2019 at 14:44

4 Answers 4

2

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

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

Comments

1

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

Comments

0

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

Comments

0

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

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.