Reputation: 1
So I am not sure why I having such a difficult time with this, but I have an object I am trying to map over to get a value from it.
For example:
let obj = {
"lyzjmpkvbtocygakcoxu": {
"label": "Show Time",
"hour": "12",
"minute": "00",
"period": "p.m.",
"add_time": "enabled",
"bdlmynfythqgyzrhruhw_add_date": "December 01, 2021",
"bdlmynfythqgyzrhruhw_stock": ""
},
"eueuuzvkteliefbnwlii": {
"label": "Show Time",
"hour": "05",
"minute": "00",
"period": "p.m.",
"add_time": "enabled",
"brtmypzvuooqhvqugjbj_add_date": "December 02, 2021",
"brtmypzvuooqhvqugjbj_stock": ""
}
}
I want to be able to get this:
December 01, 2021, December 02, 2021
I was able to do this to get the values and key for "add_time", however what I want is values for key containing the "..._add_date".
Object.keys(obj).reduce(
(acc, elem) => {
acc[elem] = obj[elem].add_time;
return acc;
},
{},
);
Upvotes: 0
Views: 39
Reputation: 370979
I suppose you could search through the keys and .find
the one that matches the pattern...
let obj = {
"lyzjmpkvbtocygakcoxu": {
"label": "Show Time",
"hour": "12",
"minute": "00",
"period": "p.m.",
"add_time": "enabled",
"bdlmynfythqgyzrhruhw_add_date": "December 01, 2021",
"bdlmynfythqgyzrhruhw_stock": ""
},
"eueuuzvkteliefbnwlii": {
"label": "Show Time",
"hour": "05",
"minute": "00",
"period": "p.m.",
"add_time": "enabled",
"brtmypzvuooqhvqugjbj_add_date": "December 02, 2021",
"brtmypzvuooqhvqugjbj_stock": ""
}
}
const result = Object.values(obj)
.map(val =>
Object.entries(val)
.find(([key]) => key.endsWith('_add_date'))
[1]
)
.join(', ');
console.log(result);
But a much better approach would be to fix the upstream code so it doesn't create such a strange structure to begin with.
Upvotes: 1