Javascript 返回每个 ;选择 ;(例如,where ;selected ;为真)候选股票

Javascript 返回每个 ;选择 ;(例如,where ;selected ;为真)候选股票,javascript,typescript,Javascript,Typescript,给定一系列符合以下类型的候选人: type Candidate = { name: string, tags: string[], selected: boolean, }; 如何返回每个选定候选共享的标签列表?仅当每个选定的候选对象都有标记时,标记才会包含在结果中。输出中标记的顺序并不重要 function sharedTags(candidates) { // code goes here return []; } module.exports = sharedTag

给定一系列符合以下类型的候选人:

type Candidate = {
  name: string,
  tags: string[],
  selected: boolean,
};
如何返回每个选定候选共享的标签列表?仅当每个选定的候选对象都有标记时,标记才会包含在结果中。输出中标记的顺序并不重要

function sharedTags(candidates) {
  // code goes here
  return [];
}

module.exports = sharedTags;

让我们把你想要完成的步骤分解一下

首先,您需要确定选择了哪些候选人。您可以使用标准的
Array.filter(predicateFn)
实现这一点。您的谓词将沿着
函数(c){return c.selected==true;}
的行查找某些内容

值得一提的是,如果您可以构造代码,使
候选者
参数始终与选定候选者的数组一起提供,那么第一步就没有必要了。与软件中的大多数事情一样,这取决于您愿意做出的假设

接下来,您将计算一个集合,该集合表示候选集合之间标记的(ed:updated)交集。这涉及到编写一个助手函数,该函数可以接受两个候选项并确定它们是否有任何共同的标记:

var sharedTags = function(c1, c2) { 
    return c1.tags.filter(function(t) { 
        return c2.tags.indexOf(t) >= 0;
    });
};
例:

  • fast方法是将标签的
    Map
    作为
    标记名和
    作为
    标记频率

  • 循环浏览每个候选人的标签,并准备一张独特的标签地图。distinct标记的原因是,同一个标记可能会对单个候选项重复多次。因此,这可能会打破我们对是否所有候选人都显示标签的核心决定检查

  • 有一个全局
    map
    变量,用于跟踪标签及其频率
  • 现在,在最后,迭代
    map
    中的所有标记,并检查其频率是否正确 等于
    候选项。长度
    。如果是的话,它发生在每个候选人身上,否则就没有了

  • 这样,您只需访问每个候选人的标签一次

  • 下面是一个实现来演示相同的功能
代码:

function sharedTags(candidates) {
    var results = [];
    var map = {};
    // collect each tag's frequency in a map
    for(let i=0;i<candidates.length;++i){
        let each_candidate = candidates[i];
        if(each_candidate['selected'] === true){
            // collect all unique tags for this candidate in iteration
            let unique_tags = {};

            for(let j=0;j<each_candidate['tags'].length;++j){
                let tag = each_candidate['tags'][j];
                unique_tags[tag] = unique_tags[tag] === undefined ? 1 : unique_tags[tag];
            }

            // merge it's presence with the global "map" variable
            let this_candidate_tags = Object.keys(unique_tags);
            for(let k=0;k<this_candidate_tags.length;++k){
                if(map[this_candidate_tags[k]] === undefined){
                    map[this_candidate_tags[k]] = 1;
                }else{
                    map[this_candidate_tags[k]] += 1;
                }
            }
        }        
    }

    // now check for frequency of each tag. If it equals candidates length, that means it appeared in every candidate.
    var tags = Object.keys(map);
    for(let each_tag in tags){
        if(map[tags[each_tag]] === candidates.length){
            results.push(tags[each_tag]);
        }
    }

    return results;
  }
功能共享标签(候选){
var结果=[];
var-map={};
//在地图中收集每个标签的频率

for(让i=0;i绝对正确@Barmar!感谢您的更正-我在描述它们时总是混淆这两个术语!这将把标记推到数组中,即使布尔值“已选定”为false。您如何仅查看选中为true的标记?@JohnMatrix您可以在声明
唯一\u标记之前添加
if
条件检查。我没有投反对票。您帮了我很大的忙。我仍然在条件检查的位置上遇到问题。@JohnMatrix编辑了我的代码并添加了条件检查。Hop现在这对你很有帮助。我确信你没有否决投票,我也没有指着你。干杯:)
function sharedTags(candidates) {
    var results = [];
    var map = {};
    // collect each tag's frequency in a map
    for(let i=0;i<candidates.length;++i){
        let each_candidate = candidates[i];
        if(each_candidate['selected'] === true){
            // collect all unique tags for this candidate in iteration
            let unique_tags = {};

            for(let j=0;j<each_candidate['tags'].length;++j){
                let tag = each_candidate['tags'][j];
                unique_tags[tag] = unique_tags[tag] === undefined ? 1 : unique_tags[tag];
            }

            // merge it's presence with the global "map" variable
            let this_candidate_tags = Object.keys(unique_tags);
            for(let k=0;k<this_candidate_tags.length;++k){
                if(map[this_candidate_tags[k]] === undefined){
                    map[this_candidate_tags[k]] = 1;
                }else{
                    map[this_candidate_tags[k]] += 1;
                }
            }
        }        
    }

    // now check for frequency of each tag. If it equals candidates length, that means it appeared in every candidate.
    var tags = Object.keys(map);
    for(let each_tag in tags){
        if(map[tags[each_tag]] === candidates.length){
            results.push(tags[each_tag]);
        }
    }

    return results;
  }