2

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",
    },

]
2
  • 1
    Welcome to Stack Overflow! Please take the tour if you haven't already (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO and elsewhere, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example showing your attempt and say specifically where you're stuck. People will be glad to help. (not my downvote) Commented Jan 27, 2022 at 10:06
  • 5
    Side note: The code block at the top of your question has syntax errors. If those property names really have - in them, they must be in quotes (single or double, either is fine). Commented Jan 27, 2022 at 10:06

3 Answers 3

3

const 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",
};

const myObjects = {};
Object.keys(data).forEach((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 ]; 
});

const myID = 266;

// 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 ];

console.log(myObjects);
console.log(myObjectsToArray);
console.log(selectedElement);

Sign up to request clarification or add additional context in comments.

Comments

1
const 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"
};

const results = [];

function splitKey(key) {
  const indexOfDelimiter = key.lastIndexOf("-");

  return {
    id: key.substring(indexOfDelimiter + 1),
    key: key.substring(0, indexOfDelimiter)
  };
}

function getItemFromResults(id) {
  return results.find((r) => r.id === id);
}

function processKeyValuePair(id, key, value) {
  const item = getItemFromResults(id);

  if (!item) {
    results.push({ id, [key]: value });
    return;
  }

  item[key] = value;
}

for (const k in data) {
  const { id, key } = splitKey(k);
  const value = data[k];

  processKeyValuePair(id, key, value);
}

Comments

1

Here a slightly different solution, first getting all unique id's, then builiding the result array with looping over Object.entries.

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", }

const ids= [...new Set(Object.keys(data).map((el)=>{
    return el.split('-')[1]
}))];

console.log(ids) //unique id's

let result = [];

ids.forEach((i)=>{
    let obj={id: i}
    Object.entries(data).forEach((el)=>{
        if(el[0].includes(i)) obj[el[0].split('-')[0]]=el[1];
    })
    result.push(obj)
})


console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.