How to change json data format from array to array of objects with javascript?

Wel

I am trying to load data into ag-grid so I got the json data like this format:

{"my_data":{"labels":[1,2,3,...], "idx":["idx1", "idx2", ...]}}

I need it to be like this to pass it to the grid:

{"my_data":[{"labels": 1}, {"labels": 2}, ..., {"idx":"idx1"}, {"idx":"idx2"}, ...}]

Is there a fast way to do it this format or I have to loop through the data using reduce or map ?

desired format example: https://www.ag-grid.com/sample-data/monthlySales.json

I tried to load the data like this

var obj = $.parseJSON(JSON.stringify(data));
gridOptions.api.setRowData(obj.mydata.labels);
sivako

Use reduce will simplify.

const data = { my_data: { labels: [1, 2, 3], idx: ["idx1", "idx2"] } };

const updated = Object.entries(data.my_data).reduce((acc, [key, value]) => {
  value.forEach(item => acc.push({ [key]: item }));
  return acc;
}, []);

const data_updated = { my_data: updated };

console.log(data_updated);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Make json from array of objects javascript

分類Dev

Pluck properties from JSON into array of objects in JavaScript

分類Dev

How to get array from JSON data and convert it into desired new array format?

分類Dev

How to remove duplicates objects from array in javascript?

分類Dev

Properly format JSON array in Javascript

分類Dev

Android: How to parse JSON Array of Array of objects

分類Dev

How to create an array of objects in JavaScript?

分類Dev

How to delegate to an array of objects in JavaScript

分類Dev

convert POST array data to json format

分類Dev

Combining same objects in json array javascript

分類Dev

How to get json value from json array using javascript

分類Dev

How to get unique an array of objects back from a complex an array of objects?

分類Dev

How to Pass PHP Array to Javascript as JSON Array

分類Dev

How to create array from json data in shell script

分類Dev

How do I get data from JSON Array MSSQL

分類Dev

How get data from array in json item using angularjs

分類Dev

transform an array of json objects to array

分類Dev

How to convert javascript array to specific list of objects

分類Dev

how to remove array from another array in javascript

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

How to extract a property from array of objects and slice it?

分類Dev

How to put values of objects from an array into a string

分類Dev

javascript array of objects - redistribute

分類Dev

Javascript, Array of Objects?

分類Dev

How to echo data from array?

分類Dev

How do I format an array of objects by iteration in Model in Ruby on Rails

分類Dev

How to improve quality of my javascript - given 2 json-format array

分類Dev

Displaying php curl Json Array Data in Javascript

分類Dev

How to format an array to a particular data frame format in pandas?

Related 関連記事

  1. 1

    Make json from array of objects javascript

  2. 2

    Pluck properties from JSON into array of objects in JavaScript

  3. 3

    How to get array from JSON data and convert it into desired new array format?

  4. 4

    How to remove duplicates objects from array in javascript?

  5. 5

    Properly format JSON array in Javascript

  6. 6

    Android: How to parse JSON Array of Array of objects

  7. 7

    How to create an array of objects in JavaScript?

  8. 8

    How to delegate to an array of objects in JavaScript

  9. 9

    convert POST array data to json format

  10. 10

    Combining same objects in json array javascript

  11. 11

    How to get json value from json array using javascript

  12. 12

    How to get unique an array of objects back from a complex an array of objects?

  13. 13

    How to Pass PHP Array to Javascript as JSON Array

  14. 14

    How to create array from json data in shell script

  15. 15

    How do I get data from JSON Array MSSQL

  16. 16

    How get data from array in json item using angularjs

  17. 17

    transform an array of json objects to array

  18. 18

    How to convert javascript array to specific list of objects

  19. 19

    how to remove array from another array in javascript

  20. 20

    How to get an Array of Objects from Firestore in Swift?

  21. 21

    How to extract a property from array of objects and slice it?

  22. 22

    How to put values of objects from an array into a string

  23. 23

    javascript array of objects - redistribute

  24. 24

    Javascript, Array of Objects?

  25. 25

    How to echo data from array?

  26. 26

    How do I format an array of objects by iteration in Model in Ruby on Rails

  27. 27

    How to improve quality of my javascript - given 2 json-format array

  28. 28

    Displaying php curl Json Array Data in Javascript

  29. 29

    How to format an array to a particular data frame format in pandas?

ホットタグ

アーカイブ