How to convert an object to array of multiple objects in javascript

RedHat

I have an object of data and I want to split it array of objects

let data = {
    "education_center-266": "Software House x",
    "education_center-267": "Learning Academy xyz",
    "end_date-266": "2022-01-26",
    "end_date-267": "2021-01-22",
    "start_date-266": "2021-01-26",
    "start_date-267": "1998-11-26",
    "title-266": "Web Developer",
    "title-267": "Teacher",
}

I tried differents ways but couldn't reach the result I want.. the result should be

[
    {
        id: "266",
        education_center: "Software House x",
        title: "Web Developer",
        start_date: "2021-01-26",
        end_date: "2022-01-26",
    },
    {
        id: "267",
        education_center: "Learning Academy xyz",
        title: "Teacher",
        start_date: "1998-11-26",
        end_date: "2021-01-22",
    },

]
savageGoat
const myObjects = {};
Object.keys(data).map((key) => {
    const splitKey = key.split('-');
    const elemId = splitKey[1];
    const realKey = splitKey[0];

    if (!myObjects[ elemId ]) {
        myObjects[ elemId ] = { id: elemId }; // Create entry
    }
    myObjects[ elemId ][ realKey ] = data[ key ]; 
});
// Turn into array
const myObjectsToArray = Object.values(myObjects);
// Or use the myObjects as a key/value store with ID as index
const selectedElement = myObjects[ myID ];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert object containing Objects into array of objects

How do I convert array of Objects into one Object in JavaScript?

Convert object with multiple arrays to array of objects

How do I convert an array of objects into an object (dynamically) in JavaScript

How to convert multiple objects to a single array and count the length of the objects in javascript?

How do I convert array of objects to object in javascript?

how to convert object into array of objects (or collection of objects)

How to convert objects containing array into object of objects

How to Convert an Array of Objects into an Object of Associative Arrays in Javascript

Convert Array of Objects to Nested Object in Javascript

How to convert an object into an array of objects?

How to convert array of objects to object of array in javascript?

Convert Object properties to multiple object array in JavaScript

How do I convert array of Objects to one Object in JavaScript?

How to check in JavaScript if object is in array with multiple objects?

how to convert objects in array which is already in a nested object in javaScript

How to convert object into array of objects

How to convert JavaScript array (or delimited string) to an object with sub-objects

How to convert the object which contains multiple array into an array of objects in javascript?

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to convert the given object with objects to an array with objects

How to convert an array of objects to object

Convert a array of objects to a multiple keys of object

How to convert multiple objects in an array to a new object?

How to convert a Json object with multiple objects in it (legacy) to a Json array

How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

convert an object to multiple objects in javascript

Convert object of objects to array of objects in javascript

convert multi object to array of multiple object in javascript