0

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?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

3 Answers3

1

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

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.

Yarick
  • 326
  • 3
  • 14
0

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;
}, {});
teach stem
  • 132
  • 6