简体   繁体   English

以数组格式插入值以在JavaScript中键入对象

[英]insert values in array format for key in object in JavaScript

I am trying to convert an array(with email addresses) in to object. 我正在尝试将数组(带有电子邮件地址)转换为对象。 How to insert values in value array for one key? 如何在值数组中为一个键插入值?

 var list = [ "john@yahoo.com", "rami@gmail.com", "josh@yahoo.com", "bale@gmail.com" ]; (function() { var obj1 = {}; for (var a = 0, b = list.length; b > a; a++) { var str = list[a].split("@"); var arr = []; arr.push(str[0]); if (!(str[1] in obj1)) { obj1[str[1]] = []; //arr.push(str[0])]; } Object.values(obj1[str[1]]).push(str[0]) }; console.log(obj1); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
expected output 预期产量

    {
    "gmail.com" : [3],//1+1+1
    "yahoo.com" : [4]//1+1+1+1
    }

I also want to add like 我也想添加

  { "gmail.com" : [3],//1+1+1 "yahoo.com" : [4]//1+1+1+1 } 

 var list = [ "john@yahoo.com", "rami@gmail.com", "josh@yahoo.com", "bale@gmail.com" ]; obj = {}; list.map(x => x.split('@')[1]).forEach(x => obj[x] = []) list.forEach(email => obj[email.split('@')[1]].push(email)) console.log(obj) /* { "yahoo.com": [ "john@yahoo.com", "josh@yahoo.com" ], "gmail.com": [ "rami@gmail.com", "bale@gmail.com" ] } */ 

Explanation : Created a blank object obj . 说明 :创建了一个空白对象obj Then I iterated on list and retrieved all the domains by list.map(x => x.split('@')[1]) . 然后我遍历list并通过list.map(x => x.split('@')[1])检索所有域。

With domains in hand, I setup-ed the object to have the structure { 'yahoo.com': [], 'gmail.com': [] } 有了域名,我将对象设置为具有结构{ 'yahoo.com': [], 'gmail.com': [] }

Then I iterated on list again and added the email if domain contained the corresponding part, giving resultant object. 然后,我再次遍历列表,并在域包含相应部分的情况下添加了电子邮件,从而得到了对象。

Edit : It can also be done in single iteration this way: 编辑 :也可以通过这种方式在单次迭代中完成:

 var list = [ "john@yahoo.com", "rami@gmail.com", "josh@yahoo.com", "bale@gmail.com" ] obj = {} list.forEach(email => { let domain = email.split('@')[1] if (!obj[domain]) obj[domain] = [] if (obj[domain].indexOf(email) < 0) obj[domain].push(email) }) console.log(obj) 

Here, I'm iterating on list, extracting the domain, setting up the key with [] if it doens't exist and then pushing the email into that. 在这里,我在列表上进行迭代,提取域,如果不存在则使用[]设置密钥,然后将电子邮件推送到其中。 It also makes sure that no duplicate emails are pushed. 它还可以确保不会发送重复的电子邮件。

You can simply push the values in the array if the key is found in object otherwise add the array 如果在对象中找到键,则可以简单地在数组中推送值,否则添加数组

 var list = [ "john@yahoo.com", "rami@gmail.com", "josh@yahoo.com", "bale@gmail.com" ]; (function() { var obj1 = {}; for (var a = 0; a < list.length; a++) { var str = list[a].split("@"); if(obj1[str[1]]) { obj1[str[1]].push(list[a]) } else { obj1[str[1]] = [list[a]]; //arr.push(str[0])]; } }; console.log(obj1); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Your code is almost correct, there is just a minor bug, change your line: 您的代码几乎是正确的,只有一个小错误,请更改行:

Object.values(obj1[str[1]]).push(str[0])

To

obj1[str[1]].push(list[a]);

And it works fine. 而且效果很好。

 var list = [ "john@yahoo.com", "rami@gmail.com", "josh@yahoo.com", "bale@gmail.com" ]; (function() { var obj1 = {}; for (var a = 0, b = list.length; b > a; a++) { var str = list[a].split("@"); var arr = []; arr.push(str[0]); if (!(str[1] in obj1)) { obj1[str[1]] = []; //arr.push(str[0])]; } obj1[str[1]].push(list[a]); }; console.log(obj1); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Array.prototype.reduce is typically used to translate array data to object form. Array.prototype.reduce通常用于将array数据转换为object形式。

See below for a practical example 👇 参见下面的实际示例👇

 // Emails. const emailAddresses = ["bale@gmail.com", "john@yahoo.com", "rami@gmail.com","josh@yahoo.com"] // Group By Domain. const groupByDomain = addresses => addresses.reduce((acc, email) => { const [prefix, domain] = email.split(/@/) const exists = acc[domain] if (exists) acc[domain].push(email) else acc[domain] = [email] return acc }, {}) // Output. const output = groupByDomain(emailAddresses) // Proof. console.log(output) 

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

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