Reputation: 6697
Here is my code:
var parts = ['Identifiers', 'Names', 'Emails'];
window["identifier_websites"] = ["aparat", "youtube", "telegram"];
window["name_websites"] = ["twitter", "instagram", "googleplus", "facebook", "linkedin", "cloob", "stackoverflow", "hammihan", "lenzor", "delgoo"];
window["email_websites"] = ["email", "gmail", "aol", "chmail"];
And I want to make this dynamically:
{"Identifiers":identifier_websites, "Names":name_websites, "Emails":email_websites}
Is doing that possible?
Upvotes: 0
Views: 43
Reputation:
This function come here Convert Array to Object and you just add all array from a new one to merge all of them to one and use it after.
//var result = new Map(arr.map((i) => [i.key, i.val]));
var parts = ['Identifiers', 'Names', 'Emails'];
var myArrays = [];
window["identifier_websites"] = ["aparat", "youtube", "telegram"];
window["name_websites"] = ["twitter", "instagram", "googleplus", "facebook", "linkedin", "cloob", "stackoverflow", "hammihan", "lenzor", "delgoo"];
window["email_websites"] = ["email", "gmail", "aol", "chmail"];
myArrays.push(window.identifier_websites, window.name_websites, window.email_websites);
//console.log(myArrays);
var obj =myArrays.reduce(function(acc, cur, i) {
acc[parts[i]] = cur;
return acc;
}, {});
console.log(obj)
Upvotes: 1
Reputation: 132
If you're trying to dynamically walk the parts to create an object, this does it.
parts.reduce((acc, k) => {
var singular = k.substr(0, k.length - 1).toLowerCase() + "_websites";
acc[k] = window[singular];
return acc;
}, {});
Upvotes: 0
Reputation: 346
Of course, js is dynamical language, so this is not only possible, but widely used, you can try snippets like this in node REPL, just type node in terminal.
Upvotes: 0