0

I want to assign the keys in (arr1) to the strings in each nested array in (arr2). I am only a month into learning javascript, and have been stuck on this problem for 3 days trying to find a working method.

I did consult this answer - Create an object from an array of keys and an array of values

I could not figure out how to do this with Nested Arrays which is why i am reposting. Any help is greatly appreciated!!

Given the following arrays:

arr1 = ["Date","Start","High","Low","End","AdjEnd","Amount"] //KEYS I WANT TO ASSIGN TO EACH STRING
arr2 = [["1997-06-01","75","80","65","78","79","3000"],
         ["1997-06-02","70","75","60","73","74","3300"],
         ["1997-06-03","80","85","70","83","84","3800"],...] // THERE ARE 10,000+ OF THESE STRINGS

I want the output to create objects that look like this:

var newArray = [
{
  Date: "1997-06-01",
  Start: "75",
  High: "80",
  Low: "65",
  End: "78",
  AdjEnd: "79",
  Amount: "3000"
}

{
  Date: "1997-06-02",
  Start: "70",
  High: "75",
  Low: "60",
  End: "73",
  AdjEnd: "74",
  Amount: "3300"
}

{
  Date: "1997-06-03",
  Start: "80",
  High: "85",
  Low: "70",
  End: "83",
  AdjEnd: "84",
  Amount: "3800"
}

//continuing through all nested arrays

I tried this, it gives the correct format but only outputs 1 set instead of 10,000+

const obj3 = {};
arr1.forEach((element, index) => {
   obj3[element] = arr2[0][index];
 });
console.log(obj3);

I also tried this but it only outputs the "Date" to the first string and doesn't convert the nested arrays into objects.

function setID(item, index) {
  var fullnames = 
  "Date: " + item[0]
  "Start: " + item[1]
  "High: " + item[2]
  "Low: " + item[3]
  "End: " + item[4]
  "AdjEnd: " + item[5]
  "Amount: " + item[6];
  return fullnames;
}

var output = arr2.map(setID);
console.log(output);

Any help is appreciated!

pl9nt
  • 15
  • 4

0 Answers0