Here is my object in a map function:
element: {2021-12-03: Array(12)}
How to I access the Array?
I've tried this:
element.date
But I get 'undefined'
Here is my object in a map function:
element: {2021-12-03: Array(12)}
How to I access the Array?
I've tried this:
element.date
But I get 'undefined'
To access properties of an object where the property name contains problematic characters like e.g. .
, ()
, #
, -
, +
, *
, /
or
(space), use the square bracket []
notation and pass in the name of the property as a string:
element['2021-12-03'] // returns your array
I'd recommend restructuring your element such that:
element: {
date: '2021-12-03',
items: [....] // Array(12)....
}
then if you had something like an array of element you could find a specific date via:
my_element = elements.find(x => x.date === '2021-12-03')
You can restructure as @iamaword said. like this:
let data = {'2021-12-03': [1,2,3,4], '2021-11-03': [5,6,7,8]}
let elements = Object.entries(data).map(item => {
return {date: item[0], items: item[1]} // Depends on you.
});
elements.forEach(element => {
console.log(`${element.date}:${JSON.stringify(element.items)}`);
});