简体   繁体   English

通过数组键匹配对象 - Lodash

[英]Match object by array keys - Lodash

Hello i have an array of objects that looks like:你好,我有一个对象数组,看起来像:

[
    {
        "id": "ae588a6b-4540-5714-bfe2-a5c2a65f547b",
        "name": "Peter casa",
        "skills": [
            "javascript",
            "es6",
            "nodejs"
        ]
    },
    {
        "id": "ae588a6b-4540-5714-bfe2-a5c2a65f547a",
        "name": "Peter Coder",
        "skills": [
            "javascript",
            "es6",
            "nodejs",
            "express"
        ]
    }
]

and i'm trying to get the best match based on skills, i'm using lodash and the idea is resolve:我正在尝试根据技能获得最佳匹配,我正在使用 lodash,这个想法是解决:

passing "skills": [ "javascript", "es6", "nodejs", "express" ] get "Peter Coder" instead of "Peter casa" because the first one match all the skills in the array given, is there any lodash method to perform this?传递 "skills": [ "javascript", "es6", "nodejs", "express" ] 得到 "Peter Coder" 而不是 "Peter casa" 因为第一个匹配给定数组中的所有技能,是否有任何 lodash执行此操作的方法?

Edit:编辑:

In this particular example, the function needs to get the candidate with is a better fit, for instance, if i pass skills: "skills": [ "javascript", "es6", "nodejs" ] even if it is a perfect match for the first one i need to choose the second because it matches the skills and has more skills, so is a better match在这个特定的例子中,函数需要让候选人更适合,例如,如果我通过技能:“技能”:[“javascript”,“es6”,“nodejs”]即使它是一个完美的匹配对于第一个我需要选择第二个,因为它匹配技能并且拥有更多技能,因此更好的匹配

With lodash you could use intersection as @Tuan Anh Tran suggested:使用 lodash,您可以使用 @Tuan Anh Tran 建议的intersection

const users = [
  {
    id: 'ae588a6b-4540-5714-bfe2-a5c2a65f547b',
    name: 'Peter casa',
    skills: ['javascript', 'es6', 'nodejs'],
  },
  {
    id: 'ae588a6b-4540-5714-bfe2-a5c2a65f547a',
    name: 'Peter Coder',
    skills: ['javascript', 'es6', 'nodejs', 'express'],
  },
]
let skillsByUsers = {}
const skills = ['javascript', 'es6', 'nodejs', 'express']

users.map(user => {
    const skillValue = _.intersection(user.skills, skills).length
    skillsByUsersTwo = {
        ...skillsByUsersTwo,
        [user.id]: skillValue
    }
})

Without lodash:没有 lodash:

let skillsByUsers = {}
const skills = ['javascript', 'es6', 'nodejs', 'express']

skills.map(skill => {
  users.map(user => {
    if (user.skills.includes(skill)) {
      const userSkillValue = skillsByUsers[user.id] || 0
      skillsByUsers = {
        ...skillsByUsers,
        [user.id]: userSkillValue + 1,
      }
    }
  })
})

they print exactly the same output:他们打印完全相同的输出:

lodash
{
  'ae588a6b-4540-5714-bfe2-a5c2a65f547b': 3,
  'ae588a6b-4540-5714-bfe2-a5c2a65f547a': 4
}
without lodash
{
  'ae588a6b-4540-5714-bfe2-a5c2a65f547b': 3,
  'ae588a6b-4540-5714-bfe2-a5c2a65f547a': 4
}

lodash has a method call intersection https://lodash.com/docs/#intersection lodash 有一个方法调用intersection https://lodash.com/docs/#intersection

you can pass the array of skill and intersect it with each developer's skill.您可以传递技能数组并将其与每个开发人员的技能相交。 => get the length of that intersection as score and return the one with highest score. => 获取该交叉点的长度作为分数并返回得分最高的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM