1

I want to convert two arrays into another array format.

Here is my code:

var array1 = [ "Kolkata", "Begumpet", "Bellary"];

var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]],
              [[0,"Begumpet1"],[10, "Begumpet2e"]],
              [[30, "Bellary1"],[0, "Bellary2a"]]]

console.log(array2);
var resultvalue = [];
for (i = 0; i < array2.length; i++) {
    var result = "";
    var m = 0;
    for (j = 0; j < array2[i].length; j++) {
        if(m==0){
            result += "label: '" + array1[i] + "',";
            m++;
        }
        result += " '" + array2[i][j][1] + "': " + array2[i][j][0] + ", ";
    }
    resultvalue.push(result);
}
console.log(resultvalue);

It's producing what I want, except that the result is a string, while I need the actual array.

The expected output object should be like this:

dataset = [
    {label:"Kolkata", "Kolkata1":20, "Kolkata2c":10},
    {label:"Begumpet", "Begumpet1":0, "Begumpet2e":10},
    {label:"Bellary", "Bellary1":30, "Bellary2a":0},
];
6
  • Where did KOTAK come from? Isn't it suppose to be Kolkata in output you desire? Commented Nov 28, 2016 at 10:36
  • Sorry Tirthraj Barot. that is "Kolkata". by mistake i gave like that Commented Nov 28, 2016 at 10:39
  • Moreover according to me, any values like "Kotak":25 must be an object within an object... So according to what I felt to be valid, I m putting my code below. Commented Nov 28, 2016 at 10:42
  • Alright @G Boomanikandan, I have added my answer..! Commented Nov 28, 2016 at 10:47
  • Note that this has nothing to do with JSON, which is a text format. This is about converting one set of arrays to another array of objects. JSON is not involved. Commented Nov 28, 2016 at 10:59

5 Answers 5

2

You could iterate array2 and map a new object for each entry.

var array1 = ["Kolkata", "Begumpet", "Bellary"],
    array2 = [[[20, "Kolkata1"], [10, "Kolkata2c"]], [[0, "Begumpet1"], [10, "Begumpet2e"]], [[30, "Bellary1"], [0, "Bellary2a"]]],
    dataset = array2.map(function (a, i) {
        var o = { label: array1[i] };
        a.forEach(function (b) {
            o[b[1]] = b[0];
        });
        return o;
    });

console.log(dataset);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

Another solution using Array.prototype.reduce and forEach on the inner arrays to get the required array - demo below:

var array1 = [ "Kolkata", "Begumpet", "Bellary"];
var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]],[[0,"Begumpet1"],[10, "Begumpet2e"]],[[30, "Bellary1"],[0, "Bellary2a"]]];

var result = array2.reduce(function(p,c,i){
  var elem = {label : array1[i]};
  c.forEach(e => elem[e[1]] = e[0]);
  p.push(elem);
  return p;
},[]);

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

Comments

1

You could do it with this ES6 code:

var array1 = [ "Kolkata", "Begumpet", "Bellary"];
var array2 = [[[20,"Kolkata1"],[10,"Kolkata2c"]],
              [[0,"Begumpet1"],[10, "Begumpet2e"]],[[30, "Bellary1"],[0, "Bellary2a"]]]

var result = array2.map( (pairs, i) => 
    Object.assign ({ label: array1[i] }, ...pairs.map ( ([v, k]) => ({ [k]: v }))));
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

3 Comments

Thank you @trincot
You're welcome. Please note that you can only accept one answer (it seems you are trying to accept more than one).
hi trincot. i can agree but every response is correct that why i confused. but i learn More. every guys way of thinking is different. so i agree all response. thank you guys. thank you so much.
1

There were some corrections in your desired output JSON object according to the best practices of structure of JSON. So According to what I felt to be valid, here is the correct code.

Moreover I have used JSON.stringify() to print the JSON object.

And I also have modified the way you were building the output JSON object.

var array1 = ["Kolkata", "Begumpet", "Bellary"];

var array2 = [
  [
    [20, "Kolkata1"],
    [10, "Kolkata2c"]
  ],
  [
    [0, "Begumpet1"],
    [10, "Begumpet2e"]
  ],
  [
    [30, "Bellary1"],
    [0, "Bellary2a"]
  ]
];

var resultvalue = [];
for (i = 0; i < array1.length; i++) {
  var result = {};
  var temp = {};
  temp.label = array1[i];
  for (j = 0; j < 2; j++) {
    temp[[array2[i][j][1]]] = array2[i][j][0];
  }
  resultvalue.push(temp);
}

document.write(JSON.stringify(resultvalue));
console.log(resultvalue);

SORRY.... COULDN'T UNDERSTAND THE QUESTION... I UPDATED THE CODE.

Comments

0

To convert a javascript object to JSON you have to call the JSON.stringify method.

var jsonString = JSON.stringify(array2);

2 Comments

You don't seem to have read beyond the question title. The OP is actually asking how to transform the data into an array of objects.
hi Cubi, i want to convert string to JSON but i dont want json to string to conversion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.