The original idea behind the problem is that the API I'm working with returns one type of a response for GET, and requires another type to do POST / PUT. The object I'm receiving from the GET has a format like this:
const rawObject = {
data1: {
id: 1,
value: 444
},
data2: null
}
In order to be able to update it, or do a POST request to create a new one, I had to convert it into this format (This is the final output of my solution as well):
const convertedObject = {
data1: 444,
data2: null
};
The object I'm converting consists of another objects within it (One level max - as in the first example) or null. If the object is not null, I only care about the value of a nested object, therefore I flatten it as - data1: 444.
The solution I came up with is this (It works perfectly):
const rawObject = {
data1: {
id: 1,
value: 444
},
data2: null
}
let convertedObject: any = {};
Object.entries(rawObject).map((item: any) => {
const rawObjectKey = item[0];
const rawObjectValue = item[1];
if (rawObjectValue) {
convertedObject = {...convertedObject, ...{[rawObjectKey]: rawObjectValue.value}};
return;
}
convertedObject = {...convertedObject, ...{[rawObjectKey]: null}};
return;
});
console.log(convertedObject); // Check the Output in example above
I decided to use Object.entries() as I need both key:value pairs available. The if check I'm doing is there because object could have null value, therefore rawObjectValue would be null.