【问题标题】:Compare two JSON and create a json with the common value比较两个 JSON 并创建一个具有共同值的 json
【发布时间】:2020-11-10 20:08:44
【问题描述】:

我想通过比较两个 JSON 来创建两个基于公共属性值的 JSON 数组。

this.linkedParticipants =[
  {
    "id": 3,
    "name": "Participant 2",
    "email": "participant2@fico.com"
  },
  {
    "id": 2,
    "name": "Participant 1",
    "email": "participant1@fico.com"
  },
  {
    "id": 1,
    "name": "Libin Varghese",
    "email": "LibinVarghese@fico.com"
  },
  {
    "id": 7,
    "name": "Participant 5",
    "email": "participant5@fico.com"
  }
]
this.appointmentList = [
  {
    "id": 32,
    "participant": {
      "id": 1,
      "name": "Libin Varghese",
      "email": "LibinVarghese@fico.com"
    }
  },
  {
    "id": 33,
    "participant": {
      "id": 7,
      "name": "Participant 5",
      "email": "participant5@fico.com"
    }
  }
]
this.invitedList = [];
this.confirmedList = [];
let temp = {}
this.linkedParticipants.forEach((participant, i) => {
  this.appointmentList.forEach((appointment, j) => {
    if (appointment.participant.name.indexOf(participant.name)) {
      temp = {
        'participant': participant
      }
      this.invitedList.push(temp);
    }
    else {
      this.confirmedList.push(appointment)
    }
  })
})

但代码没有按预期工作,因为 this.invitedList 的两个值给出了重复值。我认为我的比较条件存在一些问题。

【问题讨论】:

  • indexOf 返回字符串中子字符串的索引。在这里,索引是0,这是假的。当名称匹配时,您的 if 条件不会通过。您可能想使用includes 而不是indexOf
  • @blex 但我仍然收到重复项

标签: javascript jquery typescript


【解决方案1】:

可以使用过滤器和比较器功能来完成。

 var linkedParticipants =[
      {
        "id": 3,
        "name": "Participant 2",
        "email": "participant2@fico.com"
      },
      {
        "id": 2,
        "name": "Participant 1",
        "email": "participant1@fico.com"
      },
      {
        "id": 1,
        "name": "Libin Varghese",
        "email": "LibinVarghese@fico.com"
      },
      {
        "id": 7,
        "name": "Participant 5",
        "email": "participant5@fico.com"
      }
    ]
    var appointmentList = [
      {
        "id": 32,
        "participant": {
          "id": 1,
          "name": "Libin Varghese",
          "email": "LibinVarghese@fico.com"
        }
      },
      {
        "id": 33,
        "participant": {
          "id": 7,
          "name": "Participant 5",
          "email": "participant5@fico.com"
        }
      }
    ]


    function comparer(otherArray){
        return function (current) {
            return otherArray.filter(function(other) {
                return other.id === current.id
            }).length===0;
        }
    }

   

   var invitedList = appointmentList.map(i=> {return i.participant});


    var confirmedList=linkedParticipants.filter(comparer(invitedList ));
    
    console.log(invitedList);
    console.log(confirmedList)

【讨论】:

  • 不幸的是,我想在没有任何库的情况下使用 js 或 ts。有什么办法可以做到吗?
【解决方案2】:

您可以通过filter() 的不同组合来实现这一点:

 var linkedParticipants =[
  {
    "id": 3,
    "name": "Participant 2",
    "email": "participant2@fico.com"
  },
  {
    "id": 2,
    "name": "Participant 1",
    "email": "participant1@fico.com"
  },
  {
    "id": 1,
    "name": "Libin Varghese",
    "email": "LibinVarghese@fico.com"
  },
  {
    "id": 7,
    "name": "Participant 5",
    "email": "participant5@fico.com"
  }
];
var appointmentList = [
  {
    "id": 32,
    "participant": {
      "id": 1,
      "name": "Libin Varghese",
      "email": "LibinVarghese@fico.com"
    }
  },
  {
    "id": 33,
    "participant": {
      "id": 7,
      "name": "Participant 5",
      "email": "participant5@fico.com"
    }
  }
];


var confirmedList = appointmentList.filter((a) => {
  return linkedParticipants.find((p) => p.id === a.participant.id);
});

var invitedList = linkedParticipants.filter((p) => {
  return !appointmentList.find((a) => p.id === a.participant.id);
});

console.log(confirmedList)
console.log(invitedList)

【讨论】:

    【解决方案3】:

    您可以考虑使用Set 来查找交叉点。 常见的做法是这样的:

    function intersect(a, b) {
      const setB = new Set(b);
      return [...new Set(a)].filter(x => setB.has(x));
    }
    

    因此,关于您的问题,您可以通过以下方式完成您的任务:

    function intersect(linkedParticipants, appointmentList) {
      const setB = new Set(linkedParticipants);
      return [...new Set(appointmentList)].filter(({participant}) => setB.has(participant));
    }
    

    我通过以下方式创建数组:

    const id1 = {
        "id": 1,
        "name": "Libin Varghese",
        "email": "LibinVarghese@fico.com"
      };
    const id2 = {
        "id": 2,
        "name": "Participant 1",
        "email": "participant1@fico.com"
      };
    const id3 = {
        "id": 3,
        "name": "Participant 2",
        "email": "participant2@fico.com"
      };
    const id7 = {
        "id": 7,
        "name": "Participant 5",
        "email": "participant5@fico.com"
      };
    const linkedParticipants =[id1, id2, id3, id7]
    const appointmentList = [
      {
        "id": 32,
        "participant": id1
      },
      {
        "id": 33,
        "participant": id7
      }
    ];
    

    https://i.stack.imgur.com/oqtZw.png 请记住,该对象是引用类型。

    【讨论】:

    • 能否请您告诉我我们如何从中创建受邀列表 = []、确认列表 = [] 数组。
    • 更新了答案。我假设您在两个数组中使用相同的对象而不是创建新的对象
    猜你喜欢
    • 1970-01-01
    • 2020-06-04
    • 2020-12-28
    • 2020-07-22
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多