简体   繁体   English

Javascript 比较对象的 2 个 arrays 找到匹配值并进行更改

[英]Javascript compare 2 arrays of objects find matching values and make change

var nickNames = [
  {
    nickName:"Dad",
    emailAddress:"dad@dad.com",
  },
  {
    nickName:"Mom",
    emailAddress:"mom@mom.com",
  },
  {
    nickName:"BFF",
    emailAddress:"bff@bff.com",
  }
]

var emails = [
  {
    from:"Dad Dadson <dad@dad.com>"
  },
  {
    from:"Mom Dadson <mom@mom.com>"
  },
  {
    from:"Brother Dadson <bro@bro.com>"
  }
]

for (var i=0; i < emails.length; i++) {
  emails[i].from.replace(/ *\<[^)]*\> */g, "")
}

for (var i=0; i < emails.length; i++) {
  if (emails.find(email => email.from) === nickNames.find(nick => nick.emailAddress)) {
    emails[i].nickName = nick.nickName
  }
}

Trying to accomplish 2 things:试图完成两件事:

  1. Compare both arrays and see if there are any matches between:比较两者 arrays 并查看是否有任何匹配:
nickNames.emailAddress

and

emails.from

The regex expression to isolate the email address works in a separate area of my code, but isn't accomplishing it here.用于隔离 email 地址的正则表达式在我的代码的单独区域中工作,但在这里没有完成。

  1. If there is a match, create a new key value pair in the matching emails array element, with the corresponding nickname.如果匹配,则在匹配的 emails 数组元素中创建一个新的键值对,并带有相应的昵称。

Desired result:期望的结果:

emails = [
  {
    from:"dad@dad.com",
    nickName: "Dad"
  },
  {
    from:"mom@mom.com",
    nickName: "Mom"
  },
  {
    from:"bro@bro.com"
  }
]

Thank you for any assistance you can provide!感谢您提供的任何帮助!

It seems your regex is removing the email address from the from property, rather than preserving it.看来您的正则表达式正在从from属性中删除 email 地址,而不是保留它。 Here's a sample with a different regex that pulls out the email during comparison and ignores the rest.这是一个具有不同正则表达式的示例,它在比较期间拉出 email 并忽略 rest。

 const nickNames = [ { nickName:"Dad", emailAddress:"dad@dad.com", }, { nickName:"Mom", emailAddress:"mom@mom.com", }, { nickName:"BFF", emailAddress:"bff@bff.com", } ] let emails = [ { from:"Dad Dadson <dad@dad.com>" }, { from:"Mom Dadson <mom@mom.com>" }, { from:"Brother Dadson <bro@bro.com>" } ] emails.forEach(email => { const match = nickNames.find(nickName => nickName.emailAddress === email.from.replace(/^(.* )<(.+)>$/, "$2")); if (match) { email.nickName = match.nickName; } }); console.log(emails);

The desired output can be obtained even without regex-即使没有正则表达式,也可以获得所需的 output -

var nickNames = [
  {
nickName:"Dad",
emailAddress:"dad@dad.com",
  },
  {
nickName:"Mom",
emailAddress:"mom@mom.com",
  },
  {
nickName:"BFF",
emailAddress:"bff@bff.com",
  }
]

var emails = [
  {
from:"Dad Dadson <dad@dad.com>"
  },
  {
from:"Mom Dadson <mom@mom.com>"
  },
  {
from:"Brother Dadson <bro@bro.com>"
  }
]

nickNames.forEach(item=>{
    emails.forEach((entry)=>{
        if(entry.from.includes(item.emailAddress))
        {
            entry.nickname = item.nickName;
        }
    });
});

console.log(emails);
function mapEmailsToNickNames(emails, nickNames) {

  // With this function we'll create
  // a mapping from emailAddress to nickName
  const addKeyValue = (map, { nickName, emailAddress }) => {
    map.set(emailAddress, nickName);
    return map;
  };

  // Learn about Array#reduce it's super useful
  const mapEmailsToNickNames = nickNames.reduce(addKeyValue, new Map());

  // This regex will be used to extract
  // an email from <bro@bro.com> like strings
  const regexpEmail = new RegExp(/\<(.*)\>$/);
  
  // Learn about Array#map function.
  // It's highly useful
  // basically it allows you to run a function
  // on each element of an array and replace the original element
  // with a function output
  return emails.map(({ from }) => {
    // extract email from a pattern <email>
    const email = from.match(regexpEmail)[1];

    // check to see if an email has a nickName
    const nickName = mapEmailsToNickNames.get(email);
    if (nickName) {
      return { email, nickName };
    } else {
      return { email };
    }
  });
}

const result = mapEmailsToNickNames(emails, nickNames);
console.log(result);

It's a more functional solution, and it should perform better on larger datasets.这是一个更实用的解决方案,它应该在更大的数据集上表现更好。

By the way, maybe your choice of data structures isn't the best here, try looking into using sets, maps, and object, you may find them useful when you need to search for an item.顺便说一句,也许您选择的数据结构在这里不是最好的,尝试使用集合、映射和 object,当您需要搜索项目时,您可能会发现它们很有用。

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

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