Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72358

How to insert specific extracted array elements into a new array with right structure

$
0
0

I have 1 variable which is very long (about 600 lines of code (1 line for each human)) and contains 16 different pieces of information about the 600 humans. I have to extract just 4 pieces of information and have done so by using

var foo = myArray.map(a => a.foo);
var bar = myArray.map(a => a.bar);
var foo2 = myArray.map(a => a.foo2);
var bar2 = myArray.map(a => a.bar2);

I wish to insert all these informations into a table and have written a code which is able to do so:

myArray.forEach(function(items) {
var row = document.createElement("tr");
    items.forEach(function(item) {
    var cell = document.createElement("td");
        cell.textContent = item;
        row.appendChild(cell);
    });
    table.appendChild(row);
});

My problem is now that the code above is only able to do so if if I reference to an array (example above "myArray"). I haven't figured out how I insert all of the extracted array values into another array with the right structure. I am hoping someone knows how to insert the values into an array so that it will be structured a la like this:

var newArray = [
     [foo[0], bar[0], foo2[0], bar2[0], 
     [foo[1], bar[1], foo2[1], bar2[1], 
     ...]

and so on until all 600 indexes are included.. I could do type all of this in by hand, but the many lines of code would make it take very long and so I am hoping one of you could tell me how to insert the extracted value in an array as shown above. Any help is greatly appreciated!

Part of myArray for reference:

myArray = [
  {name: "name", age: age, job: "occupation", works: "published work", famous_for: "discoveries", date_of_death: "date", death_cause: "cause", married: "yes/no", spouse: "name of spouse", country: "country", state: "state", economic_status: "poor/wealthy", buried: "graveyard", creed: "creed", race: "race", famous_words: "famous words.."},  
  {name2: "name", age2: age, job2: "occupation", works2: "published work", famous_for2: "discoveries", date_of_death2: "date", death_cause2: "cause", married2: "yes/no", spouse2: "name of spouse", country2: "country", state2: "state", economic_status2: "poor/wealthy", buried2: "graveyard", creed2: "creed", race2: "race", famous_words2: "famous words.."}, 

1 line is 1 person and there are 600 persons. I wish to only use some of the value and have extracted these values by using the

myArray.map(a => a.name)
myArray.map(a => a.famous_for)
myArray.map(a => a.creed)
myArray.map(a => a.race)

By using these values, I'm hoping the newArray would look like this:

var newArray = [
     [name: "name", famous_for: "discoveries", creed: "creed", race: "race", 
     [name: "name", famous_for: "discoveries", creed: "creed", race: "race", 
     ...]

Viewing all articles
Browse latest Browse all 72358

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>