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

问题:

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

回答1:

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


回答2:

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


回答3:

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


回答4:

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

  • 发表于 2019-07-04 20:56
  • 阅读 ( 136 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除