0

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 = [
  "[email protected]", "[email protected]",
  "[email protected]", "[email protected]"
];

(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" : ["a","b","c"],
    "yahoo.com" : ["de","e","f"]
    }

I also want to add like

    {
    "gmail.com" : [3],//1+1+1
    "yahoo.com" : [4]//1+1+1+1
    }
2
  • What is your expected output? Commented Jun 13, 2018 at 7:46
  • var list = [ "[email protected]", "[email protected]", "[email protected]", "[email protected]" ]; (function(){ var obj = {}; 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 obj)){ obj[str[1]] = arr; } //obj[str[1]].push(str[0]); document.write(str[0]); document.write('<br>'); }; console.log(obj); })(); Commented Jun 13, 2018 at 7:51

4 Answers 4

1

var list = [
  "[email protected]", "[email protected]",
  "[email protected]", "[email protected]"
];
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": [
"[email protected]",
"[email protected]"
  ],
  "gmail.com": [
"[email protected]",
"[email protected]"
  ]
}
*/

Explanation: Created a blank object obj. Then I iterated on list and retrieved all the domains by list.map(x => x.split('@')[1]).

With domains in hand, I setup-ed the object to have the structure { '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 = [
  "[email protected]", "[email protected]",
  "[email protected]", "[email protected]"
]
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.

Sign up to request clarification or add additional context in comments.

2 Comments

if you'd explain it a little bit, that would be easy for OP to grab and appreciate your effort :)
@GeorgeBailey sure :)
0

You can simply push the values in the array if the key is found in object otherwise add the array

var list = [
  "[email protected]", "[email protected]",
  "[email protected]", "[email protected]"
];

(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>

Comments

0

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 = [
  "[email protected]", "[email protected]",
  "[email protected]", "[email protected]"
];

(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>

Comments

0

Array.prototype.reduce is typically used to translate array data to object form.

See below for a practical example 👇

// Emails.
const emailAddresses = ["[email protected]", "[email protected]", "[email protected]","[email protected]"]

// 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.