Chartjs-Using data structures in chartjs

0👍

You can use map function for that.

Try this

const data = [
    {
        _id: 1,
        contact: [{ name: "Jane Doe", PhoneNumber: 0234567 }],
        Group: "groupName"
    },
    {
        _id: 2,
        contact: [{ name: "Jane Doe", PhoneNumber: 0234567 }],
        Group: "groupName1"
    },
    {
        _id: 3,
        contact: [{ name: "John Doe", PhoneNumber: 0234567 }],
        Group: "groupName1"
    }
];

data.map(item => {
    return {
        name: item.contact[0].name,
        phoneNumber: item.contact[0].PhoneNumber,
        Group: item.Group
    };
});

Output

[
    {
        "name": "Jane Doe",
        "phoneNumber": 80247,
        "Group": "groupName"
    },
    {
        "name": "Jane Doe",
        "phoneNumber": 80247,
        "Group": "groupName1"
    },
    {
        "name": "John Doe",
        "phoneNumber": 80247,
        "Group": "groupName1"
    }
]

Leave a comment