'How to generate array of object from object
I have an object like this:
{
name: 'ABC',
age: 12,
timing: '2021-12-30T11:12:34.033Z'
}
I want to make array of object of each key of above object like this:
[
{
fieldName: 'name',
dataType: 'string'
},
{
fieldName: 'age',
dataType: 'number'
},
{
fieldName: 'timing',
dataType: 'date'
}
]
I tried this:
let op = Object.entries(data).map(([key, value]) => ({
fieldName: key,
dataType: typeof value
}));
but getting dataType: 'object' for date column but not able to achieve it. Is there any solution? Thanks in advance.
Solution 1:[1]
You can use date parse to check is a valid date or not.
const input = {
name: 'ABC',
age: 12,
timing: '2021-12-30T11:12:34.033Z'
};
const output = Object.entries(input).map(([key, value]) => ({
fieldName: key,
dataType: typeof value === 'string' && !isNaN(Date.parse(value)) ? 'date':typeof value
}));
console.log(output);
Solution 2:[2]
You could use Object#entries with Array#map and typeof to the get the type of the value. For the case where 'date' must be used for valid time stamps, you could use Date.parse() method with Number#isNaN.
Try the following
const input = {
name: 'ABC',
age: 12,
timing: '2021-12-30T11:12:34.033Z'
};
const output = Object.entries(input).map(([key, value]) => ({
fieldName: key,
dataType: typeof value !== 'string' || isNaN(Date.parse(value)) ? typeof value : 'date'
}));
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Edit: Replace Object#values with Object#entries.
Edit 2: Add typeof value !== 'string' for dataType condition. Borrowed from @RahulSharma's answer.
Solution 3:[3]
Your solution looks fine, you just need to distinguish between normal string and a valid date string.
For this, I would recommend using dayjs package.
import dayjs from "dayjs";
const input = {
name: "ABC",
age: 12,
timing: "2021-12-30T11:12:34.033Z"
};
const output = Object.entries(input).map(([key, value]) => {
return {
fieldName: key,
dataType:
typeof value === "string"
? dayjs(value).isValid()
? "date"
: "string"
: typeof value
};
});
console.log("output", output);
If you want to reinvent the wheel and write a date checking function yourself, then take a look here for a few suggestions.
Solution 4:[4]
Object.entries(a).map(([key, value]) => (
{
fieldName: key,
dataType: isNaN(value) && !isNaN(Date.parse(value)) ? 'date' : typeof(value)
}))
But be careful with this as it will return for invalid dates in February, rg: 2013-02-31
With the date checking, you may should use some libraries such as Momentjs, . .
Solution 5:[5]
You can use Object.keys() and Array.prototype.map():
const ip = {
name: 'ABC',
age: 12,
timing: '2021-12-30T11:12:34.033Z'
};
const op = Object.keys(ip).map((key) => ({
fieldName: key,
dataType: typeof ip[key] === 'string' && !isNaN(Date.parse(ip[key])) ? 'date' : typeof ip[key]
}));
console.log(op);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Cuong Vu |
| Solution 4 | Phong Cao |
| Solution 5 |
