0

Hello I'm stuck transforming this object to an object array.

There are the number and reason field, which are currently an ',' separated string. They should be exploded into their own object. The exampel dataset and what I want to archive can be seen in the codesnippet below.

// start value
const data = {
  date: "2021-09-07 10:28:34,2021-09-08 14:45:22",
  startDate: "2021-09-07 00:00:00,2021-09-08 14:45:22"
  id: "111111"
  number: "1,9"
  reason: "Autres,Autres"

}

// what I want to archive:
  const res = [{
    date: "2021-09-07 10:28:34",
    startDate: "2021-09-07 00:00:00",
    id: 11111,
    number: 1,
    reason: "Autres"
  } {
    date: "2021-09-08 14:45:22",
    startDate: "2021-09-08 14:45:22",
    id: 11111,
    number: 9,
    reason: "Autres"
  }]

3
  • 4
    What have you tried so far to solve this on your own? .split() combined with some loops should already get you into the right direction. Commented Dec 20, 2021 at 12:43
  • 1
    @VincentMenzel Why did you completely re-write the question? What's an "object array"? And how does a snippet without any output/result but with a syntax error improve this question? Commented Dec 20, 2021 at 13:10
  • I would like to fix them however the queue is currently full so I have to wait. Commented Dec 20, 2021 at 16:58

1 Answer 1

1
  1. Project the original object with string-combined-columns into an object with splitted columns as arrays using reduce method and spread operator.

    1a. extract column names from original object by filtering out id column.

    1b. In the process of projection, count the amount of rows.

  2. Iterate over rows using the above counter, generating a new object for each row, taking relevant column value using current iteration index.

    const original = {
      date: "2021-09-07 10:28:34,2021-09-08 14:45:22",
      startDate: "2021-09-07 00:00:00,2021-09-08 14:45:22",
      id: "111111",
      number: "1,9",
      reason: "Autres,Autres"
    };
    
    function split(obj) {
      const columnNames = Object.keys(obj).filter(key => key !== "id");
      const singleWithSplitted = columnNames.reduce((result, columnName) => {
        const splittedArray = obj[columnName].split(",");
        return ({
          rowsCount: Math.max(result.rowsCount, splittedArray.length),
          table: { ...result.table,
            [columnName]: splittedArray
          }
        });
      }, {
        rowsCount: 0
      });
      const arr = [];
      for (i = 0; i < singleWithSplitted.rowsCount; i++) {
        const result = {
          id: obj.id
        };
        columnNames.forEach((columnName) => {
          result[columnName] = singleWithSplitted.table[columnName][i];
        });
        arr.push(result);
      };
      return arr;
    }
    console.log(split(original));

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

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.