2

I have this

[
{"2015":11259750.05},
{"2016":14129456.9}
] 

and I want to convert this to

[
[2015,11259750.05],
[2016,14129456.9]
] 

is there any function using javascript we can do this.

just to conclude I have a header as well which I want to include with the above result,

[
    {"label":"SecuredYear","type":"string"},
    {"label":"ValueInDh‌​s","type":"number"}
]

along with above then it should look like `

[[
  {"label":"SecuredYear","type":"string"},
  {"label":"ValueInD‌​hs","type":"number"}‌​
], [2015,11259750.05],[2016,14129456.9]
]` 

thanks

1
  • 2
    do you realize the end result you are asking for in a array of array (multidimentional array) and not a object as you mentioned in the Title Commented Dec 6, 2016 at 8:11

5 Answers 5

4

Use Array#map method with Object.keys method to generate the array and later add original array at beginning using Array#unshift method.

var data = [{
  "2015": 11259750.05
}, {
  "2016": 14129456.9
}];

// iterate over the array to generate new array
var res = data.map(function(v) {
  // get the property name from object
  var k = Object.keys(v)[0];
  // generate element of the array
  return [k, v[k]];
  // if you want to convert property name to number then
  // return [+k, v[k]];
});

// add the original array at beginning
res.unshift(data);

console.log(res);

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

Comments

0

You could iterate the given array, take the key form the object, make a new array with the data and later unshift the header to the result.

var header = [{ label: "SecuredYear", type: "string" }, { label: "ValueInDh‌​s", type: "number" }],
    data = [{ "2015": 11259750.05 }, { "2016": 14129456.9 }],
    result = data.map(function (o) {
        var k = Object.keys(o)[0];
        return [+k, o[k]];
    });

result.unshift(header);

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

Comments

0

You can loop your array and then extract each object key Object.keys(obj)[0] and its value obj[key] as shown below.

use parseint() to convert your string data (in your case the object key) into a integer

var data = [{
  "2015": 11259750.05
}, {
  "2016": 14129456.9
}];

var newData = [];

data.forEach(function(obj,index){
   var key= Object.keys(obj)[0];
   newData.push([parseInt(key,10),obj[key]]);
});

console.log(newData);

3 Comments

but how about converting "2015" to 2015
Thanks alot it fixed my issue.
Glad I could help.. happy coding
0

var data =[{"2015":11259750.05},{"2016":14129456.9}]
var res=[];

  data.map(function(a){
  Object.keys(a).forEach(function(b){
    res.push([parseInt(b),a[b]])
    })
                
    })
  console.log(res)

1 Comment

@Saleem, please add ths information to the question.
0

Try this it is working as per your expectation :

var jsonObj = [
{"2015":11259750.05},
{"2016":14129456.9}
];

var newArr = [];
for (var i in jsonObj) {
  var objKeys = Object.keys(jsonObj[i]);
  newArr.push(Object.keys(jsonObj[i]));
  newArr[i].push(jsonObj[i][objKeys[0]]);
}

console.log(newArr);

Comments

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.