【问题标题】:Check if a merged array has item from two arrays and update the merged array JS检查合并数组是否有来自两个数组的项并更新合并数组 JS
【发布时间】:2018-09-28 13:23:41
【问题描述】:

我有两个数组。我合并它并删除重复项。合并是通过考虑数组的所有键来完成的(数组 1 中对象中的所有键值必须与数组 2 中的键值匹配)。

var result1 = [{
    name: 'Sandra',
    email: 'sandra@example.com'
  },
  {
    name: 'John',
    email: 'johnny@example.com'
  },
  {
    name: 'Peter',
    email: 'peter@example.com'
  },
  {
    name: 'Bobby',
    email: 'bobby@example.com'
  },
  {
    name: 'Arun',
    email: 'arun@gmail.com'
  },
];

var result2 = [{
    name: 'John',
    email: 'johnny@example.com'
  },
  {
    name: 'Bobby',
    email: 'bobby@example.com'
  },
  {
    name: 'Arun',
    email: 'arun@example.com'
  }
];

var result= _.uniqWith(_.concat(result1, result2), _.isEqual)

现在我需要用 array1 和 array2 的每一项检查合并数组的每一项,并更新合并数组是否存在。

所以我的最终结果应该是这样的。

var result = [{
    name: 'Sandra',
    email: 'sandra@example.com',
    presentInA: true,
    presentInB: false
  },
  {
    name: 'John',
    email: 'johnny@example.com',
    presentInA: true,
    presentInB: true
  },
  {
    name: 'Peter',
    email: 'peter@example.com',
    presentInA: true,
    presentInB: false
  },
  {
    name: 'Bobby',
    email: 'bobby@example.com',
    presentInA: true,
    presentInB: true
  },
  {
    name: 'Arun',
    email: 'arun@example.com',
    presentInA: false,
    presentInB: true
  },
  {
    name: 'Arun',
    email: 'arun@gmail.com',
    presentInA: true,
    presentInB: false
  }
];

我该如何以最好的方式做到这一点?我想我可以通过遍历所有 3 个数组来做到这一点,但这样做是一种糟糕的方式。

请指教。

【问题讨论】:

  • 是的,我们也可以做jquery
  • 抱歉,我看到问题是lodash就删除了。
  • 不,它可以匹配,但如果匹配,我需要检查整个对象。

标签: javascript arrays object lodash


【解决方案1】:

你可以这样做

result.map(
    per => ({
        name: per.name,
        email: per.email,
        presentInA: _.find(
            result1, (o) => o.nombre === per.nombre && o.email === per.email
        ) ? true : false,
        presentInB: _.find(
            result2, (o) => o.nombre === per.nombre && o.email === per.email
        ) ? true : false,
    })
)

【讨论】:

  • 我可以这样做,但是当一个对象同时存在于数组 1 和数组 2 中时它会失败。返回 false
  • 你是对的。我用另一种显然可行的方法更新了我的答案
  • id 是从哪里来的?
  • 在您的问题中,几分钟前有一个 ID。在这种情况下,您可以与姓名和电子邮件进行比较,而不是与 ID 进行比较,如下所示:result1, (o) => o.nombre === per.nombre && o.email === per.email
【解决方案2】:

您可以iterate 超过result 并使用_.some 来检查其中的每个对象是否在result1result2 中(并设置相应的属性presentInApresentInB)。像这样的:

_.forEach(result, (obj) => {
   let presentInA = _.some(result1, obj);
   let presentInB = _.some(result2, obj);
   obj.presentInA = presentInA;
   obj.presentInB = presentInB;
});

var result1 = [{
    name: 'Sandra',
    email: 'sandra@example.com'
  },
  {
    name: 'John',
    email: 'johnny@example.com'
  },
  {
    name: 'Peter',
    email: 'peter@example.com'
  },
  {
    name: 'Bobby',
    email: 'bobby@example.com'
  },
];

var result2 = [{
    name: 'John',
    email: 'johnny@example.com'
  },
  {
    name: 'Bobby',
    email: 'bobby@example.com'
  },
  {
    name: 'Arun',
    email: 'arun@example.com'
  }
];

var result = _.uniqWith(_.concat(result1, result2), _.isEqual);

_.forEach(result, (obj) => {
   let presentInA = _.some(result1, obj);
   let presentInB = _.some(result2, obj);
   obj.presentInA = presentInA;
   obj.presentInB = presentInB;
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>

【讨论】:

    【解决方案3】:

    您可以使用哈希表来查找和合并骗子:

     // Merges objects with the same name and email, sets the [arrName] property of the merged object to true
     // also takes an optional settings object for chaining
     function merge(arr, arrName, { hash = {}, result = [] } = {}) {
       for(const { name, email } of arr) {
          // Build up a hopefully unique key
         const key = name + "@" + email;
         // If its not in the hash yet, add it to the hash and the result
         if(!hash[key]) 
            result.push(hash[key] = { name, email });
         // then set the key
         hash[key][arrName] = true;
      }
      // allow chaining by exposing both hash and result
      return { hash, result, merge: (arr, arrName) => merge(arr, arrName, { hash, result }) };
     }
    

    这可以用作:

     const { result } = merge(result1, "presentInA").merge(result2, "presentInB");
    

    这是 O(n),但假设电子邮件不包含两个 @s。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-03
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多