Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to convert this data into the given format

I was trying to make the data I'm having with me to a much more readable and easily understandable format. But I was stuck with it. is it possible to convert it into a readable format?

The data I'm having is in the format below

[
      {
        "c": [
          {
            "v": "Food Truck: The Vegan"
          },
          {
            "v": "06/02/2022 16:00:00"
          },
          {
            "v": "06/02/2022 21:00:00"
          },
          {
            "v": "06/02/2022"
          }
        ]
      },
      {
        "c": [
          {
            "v": "Food Truck: Lob Dogs"
          },
          {
            "v": "06/03/2022 16:00:00"
          },
          {
            "v": "06/03/2022 21:00:00"
          },
          {
            "v": "06/03/2022"
          }
        ]
      }}]

I want to convert it to a much more readable format like the example given below.

        {
        "name": "Food Truck: The Vegan Table",
        "start_date": "06/02/2022 16:00:00",
        "end_date": "06/02/2022 21:00:00",
        "date": "06/02/2022"
        },
        {
        "name": "Food Truck: Lob Dogs",
        "start_date": "06/03/2022 16:00:",
        "end_date": "06/03/2022 21:00:00",
        "date": "06/03/2022"
        }
]

How should I do this in javascript?
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use Object.fromEntries with a separate keys array to map over your array and reformat each nested array.

const input = [{ "c": [{ "v": "Food Truck: The Vegan" }, { "v": "06/02/2022 16:00:00" }, { "v": "06/02/2022 21:00:00" }, { "v": "06/02/2022" }] }, { "c": [{ "v": "Food Truck: Lob Dogs" }, { "v": "06/03/2022 16:00:00" }, { "v": "06/03/2022 21:00:00" }, { "v": "06/03/2022" }] }]

const keys = ['name', 'start_date', 'end_date', 'date'];

const result = input.map(({ c }) => Object.fromEntries(
  keys.map((key, i) => [key, c[i].v])
));

console.log(result)

Or using some long-winded destructuring. (arguably the most descriptive...)

const input = [{ "c": [{ "v": "Food Truck: The Vegan" }, { "v": "06/02/2022 16:00:00" }, { "v": "06/02/2022 21:00:00" }, { "v": "06/02/2022" }] }, { "c": [{ "v": "Food Truck: Lob Dogs" }, { "v": "06/03/2022 16:00:00" }, { "v": "06/03/2022 21:00:00" }, { "v": "06/03/2022" }] }]

const result = input.map((
  { c:
    [
      { v: name },
      { v: start_date },
      { v: end_date },
      { v: date }
    ]
  }
) => (
  {
    name,
    start_date,
    end_date,
    date
  }
));

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

You just need to iterate over the raw data and restructure it in the way you desire. Since there are no proper keys for raw data, if the order or index is always the same, you can do it in the following way.

var data = [{
        "c": [{
                "v": "Food Truck: The Vegan"
            },
            {
                "v": "06/02/2022 16:00:00"
            },
            {
                "v": "06/02/2022 21:00:00"
            },
            {
                "v": "06/02/2022"
            }
        ]
    },
    {
        "c": [{
                "v": "Food Truck: Lob Dogs"
            },
            {
                "v": "06/03/2022 16:00:00"
            },
            {
                "v": "06/03/2022 21:00:00"
            },
            {
                "v": "06/03/2022"
            }
        ]
    }
];


function processData(_raw) {
    var processed = [];

    _raw.forEach(_item => {
        processed.push({
            "name": _item.c[0].v,
            "start_date": _item.c[1].v,
            "end_date": _item.c[2].v,
            "date": _item.c[3].v
        });
    });
    return processed;
}

console.log(processData(data));

about 4 years ago · Juan Pablo Isaza Report

0

Use a reduce method (format in the snippet) to map the elements of your array:

const keys = `name,start_date,end_date,date`.split(`,`);
const format = (acc, obj, i) => ( {...acc, [keys[i]]: obj.v} );
const dataFormatted = getData().map( v => v.c.reduce(format, {}) );

console.log(dataFormatted);

function getData() {
  return [ {
  "c": [
    { "v": "Food Truck: The Vegan" },
    { "v": "06/02/2022 16:00:00" },
    { "v": "06/02/2022 21:00:00" },
    { "v": "06/02/2022" } ]
  }, {
  "c": [
    { "v": "Food Truck: Lob Dogs" },
    { "v": "06/03/2022 16:00:00" },
    { "v": "06/03/2022 21:00:00" }, 
    { "v": "06/03/2022" } ]
  } ];
}
.as-console-wrapper {
    max-height: 100% !important;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!