Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert JSON data to object that contains other objects?

I want to apply my returned data to create a line chart in Vuejs

My data:

[{"A": 400},{"B": 1597},{"C": 1567}]

My template from vuejs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
    <line-chart :data="Mydata"></line-chart>
</template>

<script>
export default {
    data () {
        return {
            My data: null
        }
    }  
}
</script>

The line-chart only get data format like My data: {"A": 400,"B": 1597,"C": 1567} to draw a graph.

Expected output when added to App VUE

<template>
    <line-chart :data="Mydata"></line-chart>
</template>

<script>
export default {
    data () {
        return {
            My data: {"A": 400,"B": 1597,"C": 1567}
        }
    }  
}
</script>

>Solution :

You can use a reducer function for this:

const data = [{
  "A": 400
}, {
  "B": 1597
}, {
  "C": 1567
}].reduce((acc, curr) => {
  Object.keys(curr).forEach(key => acc[key] = curr[key]);
  return acc
}, {})

console.log(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading