I've been looking for an answer to this one, but can't seem to find any, I apologize if there already is one.
I expect an object, let's say something like this:
{
name: 'myname',
items: [
{
itemID: '1'
},
{
itemID: '2'
},
{
itemID: '3'
}
]
}
The problem is that whenever there is only one object inside "items", it looks like this:
{
name: 'myname',
items: {
itemID: '1'
}
}
This means that my forEach function will break, since "items" is no longer an array of objects. There is a lot of data and things done inside each "items" object in the forEach so I prefer not having to use both forEach and another function, but rather convert it to an array and only use forEach.
This is what I want to do:
if (items is not array) {
//Convert the object to this:
{
name: 'myname',
items: [
{
itemID: '1'
}
]
}
}
I have problems to determine if items is an array of objects or just an object (in both cases typeof items will return "object"), and also how to convert it to an array if it's not an array. Very grateful for help or tips