0

I have the below set of data that is in the format of objects within an array.


  const columns = [
    {
      "usage": 395226,
      "population": 1925117,
      "value": 21,
      "abv": "MN"
    },
    {
      "usage": 893129,
      "population": 4327541,
      "value": 21,
      "abv": "IL"
    }
  ]

I require the above set of data to be converted into below shown format. I have been stuck here for a while. I tried writing a function for converting this into the desired data format, but it didn't work.


let columns = [ 
                 ["abv", "population"], 
                 ["MN", 1925117], 
                 ["IL", 4327541] 
              ] 

It would be a great help if anyone could help me solve this. Thanks in advance :)

5

4 Answers 4

0

I would do it with a reducer:

 const cols = [
    {
      "usage": 395226,
      "population": 1925117,
      "value": 21,
      "abv": "MN"
    },
    {
      "usage": 893129,
      "population": 4327541,
      "value": 21,
      "abv": "IL"
    }
  ]

cols.reduce(function(acc, current) {
  acc.push([current.abv, current.population])
  return acc;
}, ["abv", "population"]);

Or if you don't like using push:

cols.reduce(function(acc, current) {

 acc = [
  ...acc,
  [current.abv, current.population]
 ]

 return acc;
}, ["abv", "population"]);

Example: https://jsbin.com/gudivuhuro/edit?html,js,console

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

3 Comments

You could easily use ["abv", "population"] as an initial reducer value instead of an empty array
@IgorDymov Agreed. Editing the answer.
@Senelithperera I'm glad it helped, remember to 'accept' the answer that solved your question. 👍
0

Seems your first data is static in desired output:

const columns = [
    {
      "usage": 395226,
      "population": 1925117,
      "value": 21,
      "abv": "MN"
    },
    {
      "usage": 893129,
      "population": 4327541,
      "value": 21,
      "abv": "IL"
    }
  ];
  
  let cols = [];
  if (columns.length) {
      cols.push(["abv", "population"]);
      columns.forEach(item => {
          cols.push([item.abv, item.population])
      });
  }
  console.log(cols);

Comments

0

I would end up using something like this:

const columns = [
    {
      "usage": 395226,
      "population": 1925117,
      "value": 21,
      "abv": "MN"
    },
    {
      "usage": 893129,
      "population": 4327541,
      "value": 21,
      "abv": "IL"
    }
];

let converted = columns.map((c) => [c.abv, c.population]);
converted.unshift(["abv", "population"]);

Comments

0

does this solve your problem ?

const columns = [
    {
      "usage": 395226,
      "population": 1925117,
      "value": 21,
      "abv": "MN"
    },
    {
      "usage": 893129,
      "population": 4327541,
      "value": 21,
      "abv": "IL"
    }
  ]
var k = [["abv", "population"]];
for(var i = 0; i < columns.length; i++){
    k.push([columns[i].abv, columns[i].population]);
}
console.log(k) // Expected expected output

and here is a generic Implementation if you so require that.

var names = ["abv", "population"];
var k = GetPropertiesInArray(columns, names); //[["abv", "population"]];

function GetPropertiesInArray(columns, names){
    var m = [];
    m.push(names);
    for(var i = 0; i < columns.length; i++){
        var item = [];
        for(var j in names){
            item.push(columns[i][names[j]]);
        }
        m.push(item);
    }

    return m;
}

console.log(k);//Expected output

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.