0

I have a list of emails in an array:

["[email protected]", "[email protected]", "[email protected]"]

How can I loop through the array and push each email into an object as its own property: The object would look like:

{
email1: "[email protected]",
email2: "[email protected]",
email3: "[email protected]"
}
6

9 Answers 9

5

one liner:

var object = ['[email protected]', '[email protected]', '[email protected]'].reduce((r, e, i) => (r['email'+(i+1)] = e, r), {});
Sign up to request clarification or add additional context in comments.

1 Comment

(r['email'+(i+1)] = e, r) « sneaky, I love it!
2

var inputArr = ['[email protected]', '[email protected]', '[email protected]'];
var outputObj = {};
var i;

for (i=0; i<inputArr.length; i++) {
    outputObj['email'+i] = inputArr[i];
}

console.log(outputObj);

Comments

1

This should do it:

var arr = ["[email protected]", "[email protected]", "[email protected]"];
var emails = {}; 
arr.forEach((e, i) => emails["email" + (i+1)] = e);
console.log(emails);

2 Comments

Doesn't 'i' have the value?!
@ILikeToMoveItMoveIt No, i (the second parameter) is the index. Run the snipped to see that it produces the correct result.
1

You could do this quite easily with Array#reduce:

var emails = ['[email protected]', '[email protected]', '[email protected]']

var result = emails.reduce(function (o, e, i) {
  o['email' + ++i] = e
  return o
}, {})

console.log(result)

Comments

0

You can try this

var ary = [];

function pushToAry(name, val) {
   var obj = {};
   obj[name] = val;
   ary.push(obj);
}

pushToAry("myName", "myVal"); // Here it is for single value u can loop with your array. 
  • Help

3 Comments

why you put your name at last?
@SagarV "Publicity"
@suraj (Hence the use of sarcastic quotation marks around my earlier remark)
0

Here is another solution

var emails = ["[email protected]", "[email protected]", "[email protected]"];
var object = {}; 
emails.forEach(function(value, key){
      object["email" + (key+1)] = value;
});

console.log(object);

Comments

0
obj = {};
for(i=0;i<yourArray.length;i++){
propName = 'email' + i;
obj[propName] = yourArray[i];
}

I believe this should work.

Comments

0

This is what Array.prototype.reduce is made for

const emails = ['[email protected]', '[email protected]', '[email protected]']
console.info(emails.reduce((map, email, idx) =>
  Object.assign(map, { ['email' + (idx + 1)]: email }), Object.create(null)))

2 Comments

Just curious, is Object.create(null) a style choice?
0

Please find the updated code attached below

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

var obj = {
   
};
for (var prop in array) {

obj["email"+(prop+1)] = array[prop];
}
console.log(obj);

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.