Home > other >  How to convert this data into the given format
How to convert this data into the given format

Time:06-23

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?

CodePudding user response:

Here's a map and reduce way to create an object from the first array of your objects.

const dataRaw = {
    "c": [
      { "v": "Food Truck: The Vegan" },
      { "v": "06/02/2022 16:00:00" },
      { "v": "06/02/2022 21:00:00" },
      { "v": "06/02/2022" } ]
  };
const keys = `name,start_date,end_date,date`.split(`,`);
const dataFormatted = dataRaw.c
  .map( (val, i) => ( [[keys[i]], val.v] ) )
  .reduce( (acc, [key, value]) => ({...acc, [key]: value}), {} );

console.log(dataFormatted);

CodePudding user response:

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));

CodePudding user response:

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)

CodePudding user response:

If the rawData is always the same format i would say something like this

const rawData = [
  {
    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 format = ["name", "start_date", "end_date", "date"];

const formattedResult = rawData.map(({ c }) => Object.fromEntries(
  format.map((key, i) => [key, c[i].v])
));

console.log(formattedResult);

CodePudding user response:

You can user Array.prototype.map() combined with Array.prototype.reduce()

Code:

const 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',},],},]

const keysTemplate = ['name', 'start_date', 'end_date', 'date']

const result = data.map(({ c }) =>
  c.reduce((a, { v }, i) => {
    a[keysTemplate[i]] = v
    return a
  }, {})
)

console.log(result)

  • Related