1

I have source data in the following format (object of arrays):

{
    value: [
      59, 58, 62, 58, 56, 57, 57, 65, 81, 81
    ],
    dayOfWeek: [
      'Thursday', 'Friday', 'Saturday', 'Thursday', 'Friday', 'Friday',
      'Friday',   'Monday', 'Friday',   'Tuesday'
    ],
    dayOrNight: [
      'N', 'D', 'N', 'D', 'D', 'N', 'N', 'N', 'N', 'N'
    ]
}

And I want the data transformed into the following format (array of objects):

[
    {
        value: 59,
        dayOfWeek: 'Thursday',
        dayOrNight: 'N'
    },
    {
        value: 58,
        dayOfWeek: 'Friday',
        dayOrNight: 'D'
    },
    {
        value: 62,
        dayOfWeek: 'Saturday',
        dayOrNight: 'N'
    },
    ...
]

This is kinda the opposite of this question.

What is the appropriate combination of commands to do this, whether using e.g. lodash or otherwise?

1 Answer 1

2

Combining map method and accessing by key to object, we can achieve desired result:

data.value.map((s, i) => ({value: s, 
   dayOfWeek: data.dayOfWeek[i], dayOrNight: data.dayOrNight[i]}));

An example:

let data = {
    value: [
      59, 58, 62, 58, 56, 57, 57, 65, 81, 81
    ],
    dayOfWeek: [
      'Thursday', 'Friday', 'Saturday', 'Thursday', 'Friday', 'Friday',
      'Friday',   'Monday', 'Friday',   'Tuesday'
    ],
    dayOrNight: [
      'N', 'D', 'N', 'D', 'D', 'N', 'N', 'N', 'N', 'N'
    ]
};

const result = data.value.map((s, i) => ({value: s, 
   dayOfWeek: data.dayOfWeek[i], dayOrNight: data.dayOrNight[i]}));
console.log(result);

UPDATE:

This is slightly modified your approach:

let data = {
    value: [
      59, 58, 62, 58, 56, 57, 57, 65, 81, 81
    ],
    dayOfWeek: [
      'Thursday', 'Friday', 'Saturday', 'Thursday', 'Friday', 'Friday',
      'Friday',   'Monday', 'Friday',   'Tuesday'
    ],
    dayOrNight: [
      'N', 'D', 'N', 'D', 'D', 'N', 'N', 'N', 'N', 'N'
    ]
};

const result = Object.values(data)[0].map((s, i) => {
    let obj = {};
    for (let key in data) {
            obj[key] = data[key][i];
    }
    return obj;
});
console.log(result);

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

6 Comments

neat! any way of doing this without having to include the property names? Ideally it would be generic and future-proof to deal with any number of different properties, not just those that happen to be present in the data now. Other than maybe naming the property that is used as the basis for the map function (i.e. value here)... the rest would ideally not be named specifically.
@drmrbrewer thanks! what names would you insert instead of dayOrNight, dayOfWeek, value if there would not be property names?
sorry, by "not including the property names" I mean not hard-coding them into the conversion function, but just reading them dynamically from the source data object instead. That way, if a new array is added to the data object later (with a new property name) it would automatically be handled by the conversion function still... probably just loop through the keys in the object... I'll give it a try unless you get there first!
@drmrbrewer I slightly modified your approach, please, see my updated reply.
ah yes, neatly side-stepping the arbitrary use of one of the object properties as a basis for the map function.
|

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.