0

I tried to loop array element into the object. I tried the given method but i got some errors in it. Loop object keys and their values based on the array.

let headings = ['sam', 'ram', 'ham'.....];
let dataHeading = ['headingOne', 'headingTwo', 'headingThree'....];
let data = ['one', 'two', 'three'.....];

let finaldata;
for(let i=0; i<headings.length; i++){
  finaldata = {
    heading[i]: {
      dataHeading[i] : data[i]
    }
  }
}

console.log(finaldata);


//what i want as a output is

{
  sam: {
    headingOne: one
  },
  ram: {
    headingTwo: two
  },
  ham: {
    headingThree : three
  }
}  
wang
  • 1,660
  • 9
  • 20

6 Answers6

1

You need an assignment to the wanted property and take a computed property names for the object.

let headings = ['sam', 'ram', 'ham'],
    dataHeading = ['headingOne', 'headingTwo', 'headingThree'],
    data = ['one', 'two', 'three'],
    finaldata = {};

for (let i = 0; i < headings.length; i++) {
    finaldata[headings[i]] = { [dataHeading[i]]: data[i] };
}

console.log(finaldata);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Ciao, supposing that headings, dataHeading and data have the same length you could do:

let headings = ['sam', 'ram', 'ham'];
let dataHeading = ['headingOne', 'headingTwo', 'headingThree'];
let data = ['one', 'two', 'three'];
let finaldata = {};
for(let i=0 ; i<headings.length;i++) {
   finaldata[headings[i]] = {};
   finaldata[headings[i]][dataHeading[i]] = data[i];
}
console.log(finaldata);
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
0

For dynamic property name, you could wrap it arround square bracket []

let headings = ['sam', 'ram', 'ham']
let dataHeading = ['headingOne', 'headingTwo', 'headingThree']
let data = ['one', 'two', 'three']

let finaldata = {}
for (let i = 0; i < headings.length; i++) {
  Object.assign(finaldata, {
    [headings[i]]: {
      [dataHeading[i]]: data[i]
    }
  })
}

console.log(finaldata)
hgb123
  • 13,869
  • 3
  • 20
  • 38
0

There were 3 mistakes in your code:

  1. A heading[i] typo which can be ignored as it was not a big fault.
  2. You were creating object property on the fly so you should use [] notation. More info
  3. You were changing finaldata in each iteration
finaldata = {

heading[i] : { 
dataHeading[i] : data[i]
}
}

Because of this only last element i.e ham would be stored with with "headingThree": "three" i.e {ham:{"headingThree": "three"}} if step 2 was followed

let headings = ['sam', 'ram', 'ham'];
let dataHeading = ['headingOne', 'headingTwo', 'headingThree'];
let data = ['one', 'two', 'three'];

let finaldata = {};
for (let i = 0; i < headings.length; i++) {

  finaldata[headings[i]] = {
    [dataHeading[i]]: data[i]
  }
}
console.log(finaldata)
kapil pandey
  • 1,853
  • 1
  • 13
  • 26
0

One option could be to zip all the arrays together first to get a 2d array which looks like:

[['sam', 'headingOne', 'one'], ['ram', 'headingTwo', 'two'], ...]

Once you've done that you can then map over each zipped array and use .reduceRight() to build your nested object. This will transform the above array into an array of objects:

[
  {'sam': {'headingOne': 'one'}}, 
  {'ram': {'headingTwo', 'two'}},  
  ...
]

Lastly, you can use Object.assign() with the spread syntax to merge all the objects from your array into a resulting object:

{
  'sam': {'headingOne': 'one'}, 
  'ram': {'headingTwo', 'two'},  
  ...
}

See example below:

let headings = ['sam', 'ram', 'ham']
let dataHeading = ['headingOne', 'headingTwo', 'headingThree'];
let data = ['one', 'two', 'three'];

const zip = (...arrs) => arrs[0].map((_, i) => arrs.map(arr => arr[i]))
const makeNestedObj = arr => arr.reduceRight((obj, k) => ({[k]: obj}));

const arrObjs = zip(headings, dataHeading, data).map(makeNestedObj);
const res = Object.assign({}, ...arrObjs);
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

var myObject = {},
    countryList = ["United Kingdom", "France"],
    marketList = ["primary", "secondary", "service"],
    data = ["one","two","three"],
    i, p;

for (var i = 0; i < countryList.length; i++) {
    myObject[countryList[i]] = {};
    for (p = 0; p < marketList.length; p++) {
        myObject[countryList[i]][marketList[p]] = data[p];
    }
}

console.log(myObject);
TalOrlanczyk
  • 1,205
  • 7
  • 21