1

I have an API call which returns data in such format:

const testScheduleData = [
    {
        DATE: "2021-06-20",
        SELECTED: false,
        STARTINGDAY: true,
        ENDINGDAY: true,
        STATUS: "WORK",
        COLOR: "green",
    },
    {
        DATE: "2021-06-21",
        SELECTED: false,
        STARTINGDAY: true,
        ENDINGDAY: true,
        STATUS: "HALFDAY",
        COLOR: "green",
    },
];

I need to restructure it to an object of objects in such format:

Object {
  "2021-06-20": Object {
    "color": "green",
    "endingDay": true,
    "selected": false,
    "startingDay": true,
  },
  "2021-06-21": Object {
    "color": "green",
    "endingDay": true,
    "selected": false,
    "startingDay": true,
  },

the way I do it is first to map the testScheduleData to array (ignore item.STATUS for now):

const datesArr = testScheduleData.map((item, index) => ({
    [item.DATE]: {
        startingDay: item.STARTINGDAY,
        endingDay: item.ENDINGDAY,
        color: item.COLOR,
        selected: item.SELECTED,
    },
}));

and then use reduce to create an object from it:

var datesObj = datesArr(function (result, item) {
    var key = Object.keys(item)[0];
    result[key] = item[key];
    return result;
}, {});

which returns the correct result, but seems like an overdo. Is there a way to simplify those two steps?

3
  • 1
    const datesArr = Object.assign({}, ...datesArr). But probably you should just do the mapping inside the reduce callback Commented Jun 10, 2021 at 8:07
  • Do the properties have to be camelCase? If not, then (a slightly modified version of georgs answer): Object.fromEntries(testScheduleData.map(item => { let {DATE, ...rest} = item; return [DATE, rest]; })) Commented Jun 10, 2021 at 8:15
  • Unfortunately, yes. The way api works with database is a must uppercase. The way the library works (which uses the formatted object) is a must camel case :( Commented Jun 10, 2021 at 8:20

2 Answers 2

3

try the code below, you just need to map and push into an object

let obj = {}

const testScheduleData = [
    {
        DATE: "2021-06-20",
        SELECTED: false,
        STARTINGDAY: true,
        ENDINGDAY: true,
        STATUS: "WORK",
        COLOR: "green",
    },
    {
        DATE: "2021-06-21",
        SELECTED: false,
        STARTINGDAY: true,
        ENDINGDAY: true,
        STATUS: "HALFDAY",
        COLOR: "green",
    },
];

testScheduleData.map(row => {
   obj[row.DATE] = row
})

console.log(obj)

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

1 Comment

our job is to enlighten, OP can edit as he please
2

You can have the map callback return pairs [key, value] and then apply Object.fromEntries or new Map to the result:

datesObj = Object.fromEntries(
    testScheduleData.map(it => [
        it.DATE, {
            startingDay: it.STARTINGDAY,
            endingDay: it.ENDINGDAY,
            color: it.COLOR,
            selected: it.SELECTED,
        }
    ]))

1 Comment

Neat solution! Thank you!

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.