Json data

Hi Guys,
I have a question regarding a JSON data.
Let's say I have two arrays:
  var headers = [
      "header1",
      "header2",
      "header3",
      "header4",
    ]

    var data = [
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"]

    ]

Open in new window

now what I want to do is, for each data row I want to assign header and I want the result as it below:
[{"header1": "data1", "header2": "data2", "header3": "data3", "header4":"data4"}, ]

Open in new window


And do it for all 4 rows.

I was trying something like below which didn't work:
 function tst() {
        debugger;
        jsonarray = [];
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < headers.length; j++) {
                var insert = "{" + headers[j] + ":" + data[i][i] + "},"
                jsonarray.push(
                    insert
                );
            }
        }

Open in new window


Please, advise.
Thank you.
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
Here is modified code:

var headers = [
      "header1",
      "header2",
      "header3",
      "header4",
    ]

var data = [
   ["data1", "data2", "data3", "data4"],
   ["data1", "data2", "data3", "data4"],
   ["data1", "data2", "data3", "data4"],
   ["data1", "data2", "data3", "data4"],
   ["data1", "data2", "data3", "data4"]
]

function tst() {
	debugger;
	jsonarray = [];
	var insert
	for (var i = 0; i < data.length; i++) {
		insert = '';
		for (var j = 0; j < headers.length; j++) {
			insert += "{" + headers[j] + ":" + data[i][j] + "},";
		}
		jsonarray.push(insert);
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you. it works perfectly.
0
Julian HansenCommented:
A slightly different approach that builds the array up as a JavaScript array of objects and then stringify's the result
<script>
var headers = [
      "header1",
      "header2",
      "header3",
      "header4",
    ]

    var data = [
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"],
       ["data1", "data2", "data3", "data4"]

    ]
var result = [];  
for(var i = 0; i < data.length; i++) {
  var item = {};
  for(var j = 0; j < data[i].length; j++) {
    item[headers[j%4]] = data[i][j];
  }
    
  result.push(item);
}
var json = JSON.stringify(result);
console.log(json);
</script>

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.