Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda