【问题标题】:How to compaire parts of a string in an array of objects with other strings?如何将对象数组中的部分字符串与其他字符串进行比较?
【发布时间】:2020-09-30 10:37:11
【问题描述】:

我在 javascript 中有一个对象数组,例如:

var arr_objects= [
   {id: 1, authors: "person1||person2", edithors: "person1||person7"}, 
   {id: 2, authors: "person3", edithors: "person2"}, 
   {id: 3, authors: "person4||person5||person6", edithors: "person7||person6"}, 
   {id: 4, authors: "person7||person6", edithors: "person6"}
];

我想检查“authors”中的任何名称(person1、person2 等)是否出现在“edithors”中的同一对象中。如果是这种情况 - 将新的键值对(“出现”)写入包含作者/编辑者姓名的对象。

输出应如下所示:

var arr_objects= [
   {id: 1, authors: "person1||person2", edithors: "person1||person7", occurance: "person1"}, 
   {id: 2, authors: "person3", editors: "person2" }, 
   {id: 3, authors: "person4||person5||person6", edithors: "person7||person6", occurance: "person6"}, 
   {id: 4, authors: "person7||person6", edithors: "person6", occurance: "person6"}
];

我是编程新手,似乎完全被卡住了。 我想要实现输出,我必须在“作者”和“编辑”上使用正则表达式来分隔值并将两个字符串与每个字符串进行比较。不幸的是,在比较值时,我无法使用正则表达式并循环遍历数组。

我会非常感谢任何关于我的问题的建议。

【问题讨论】:

标签: javascript arrays javascript-objects string-comparison


【解决方案1】:
arr_objects.forEach(obj => {
    const authors = obj.authors.split('||');
    const editors = obj.edithors.split('||');
    const matches = authors.filter(val => editors.includes(val));
    if (matches.length) {
        obj.occurrences = matches;
    }
});

  1. 将作者和编辑分成两个数组。
  2. 查找两个数组之间的所有重复项。
  3. 将重复项分配给您的新对象属性occurrences

【讨论】:

    【解决方案2】:

    这是一个带有一些解释的解决方案

    arr_objects.map(function(item)
    {
       // split both authors and editors into an array
       let authors = item.authors.split('||'); 
       let editors = item.edithors.split('||'); 
       let occurance = [];
       // for each author, check if it also appears among editors
       for (a in authors)
       {
          if (editors.indexOf(authors[a])>=0)
          {
              //if yes, push it into occurrence buffer
              occurance.push(authors[a]);
          }
    
       } 
    // join the array into a string
    item.occurance = occurance.join('||'); 
    return item;
    })
    

    【讨论】:

    • 谢谢 Eriks,效果很好!你用这个救了我。
    【解决方案3】:
    var arr_objects= [
    {id: 1, authors: "person1||person2", edithors: "person1||person7"}, 
    {id: 2, authors: "person3", edithors: "person2"}, 
    {id: 3, authors: "person4||person5||person6", edithors: "person7||person6"}, 
    {id: 4, authors: "person7||person6", edithors: "person6"}
    ];
    
    function getOccurance(arr){
    // clone
    arr = [...arr]
    
    arr = arr.map(a => {
        let authors = a.authors.split("||");
        let editors = a.edithors.split("||")
        let occurance = ""
        authors.forEach(auth => {
            editors.forEach(ed => {
                if(auth == ed) {
                    if(occurance.length > 0){
                        occurance += "||" + auth;
                    }else{
                        occurance += auth
                    }
                    
                }
            })
        })
        if(occurance.length > 0){
            a.occurance = occurance
        }
        return a
    })
    return arr
    }
    console.log(getOccurance(arr_objects))
    

    【讨论】:

      【解决方案4】:

      不需要正则表达式,您可以使用splitperson7||person6 字符串中获取一组人员:

      const persons = `person7||person6`.split('||');
      

      join 可以做相反的事情:

      ['person7', 'person6'].join('||'); //evaluates to person7||person6
      

      现在您所要做的就是遍历arr_objects,拆分作者和编辑,计算它们的交集并将其放入发生中:

      for(obj of arr_objects) {
        const authors = obj.authors.split('||');
        const editors = obj.edithors.split('||');
        const intersection = computeIntersection(authors, editors);
        if(intersection.length > 0) {
          obj.occurance = intersection.join('||')
        }
      }
      

      现在剩下的就是实现computeIntersection

      function computeIntersection(array1, array2) {
        let intersection = [];
        let visited = new Set();
        for(e of array1) {
          visited.add(e);
        }
        for(e of array2) {
          if(visited.has(e)) {
            intersection.push(e);
          }
        }
        return intersection;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        相关资源
        最近更新 更多