Reputation: 1676
I would like to retrieve option with key '2021-05-14' from this json-object.
{
stock: {
zip: '95014',
lastUpdated: '05/02/2021'
},
options: [
{ '2021-05-07': [Object] },
{ '2021-05-14': [Object] },
{ '2021-05-21': [Object] },
{ '2021-05-28': [Object] },
{ '2021-06-04': [Object] },
{ '2021-06-18': [Object] }
]
}
I tried:
var options;
var expirationSeries;
options = res.body.options; // res.body is the json-object
for (var key in options)
{
if (key=='2021-05-14') {
expirationSeries = options[key];
};
}
console.log(expirationSeries);
but then I get error 'undefined'.
How to do this properly?
Upvotes: 0
Views: 1164
Reputation: 35259
Using for (var key in options)
over an array will assign the indices of the array to key
variable.
You could use for..of
loop and use hasOwnProperty
to check if the object has that date as property. Since you want only one match, you could break
if a match is found
for (const o of options) {
if (o.hasOwnProperty('2021-05-14')) {
expirationSeries = o
break;
}
}
Here's a snippet:
let options = [{"2021-05-07":{foo:"bar"}},{"2021-05-14":{foo:"bar"}},{"2021-05-21":{foo:"bar"}},{"2021-05-28":{foo:"bar"}},{"2021-06-04":{foo:"bar"}},{"2021-06-18":{foo:"bar"}}],
expirationSeries;
for (const o of options) {
if (o.hasOwnProperty('2021-05-14')) {
expirationSeries = o
break;
}
}
console.log(expirationSeries)
You could use find
and hasOwnProperty
to get the object which has the specified key. If multiple objects can have that key, use filter
to get all the matches
const expirationSeries = options.find(a => a.hasOwnProperty('2021-05-21'))
const allMatches = options.filter(a => a.hasOwnProperty('2021-05-21'))
Here's a snippet:
const options = [{"2021-05-07":{foo:"bar"}},{"2021-05-14":{foo:"bar"}},{"2021-05-21":{foo:"bar"}},{"2021-05-28":{foo:"bar"}},{"2021-06-04":{foo:"bar"}},{"2021-06-18":{foo:"bar"}}],
expirationSeries = options.find(a => a.hasOwnProperty('2021-05-21')),
allMatches = options.filter(a => a.hasOwnProperty('2021-05-21'))
console.log(expirationSeries)
console.log(allMatches)
Upvotes: 1
Reputation: 68933
Instead of for...in
you can try using for...of
.
Demo:
var options;
var expirationSeries;
options = [
{ '2021-05-07': 'data 1' },
{ '2021-05-14': 'data 2' },
{ '2021-05-21': 'data 3' },
{ '2021-05-28': 'data 4' },
{ '2021-06-04': 'data 5' },
{ '2021-06-18': 'data 6' }
]
for (var obj of options)
{
var k = Object.keys(obj)[0];
if (k=='2021-05-14') {
expirationSeries = obj[k];
};
}
console.log(expirationSeries);
OR: You can try using .find()
var options = [
{ '2021-05-07': 'data 1' },
{ '2021-05-14': 'data 2' },
{ '2021-05-21': 'data 3' },
{ '2021-05-28': 'data 4' },
{ '2021-06-04': 'data 5' },
{ '2021-06-18': 'data 6' }
];
var temp = options.find(o => Object.keys(o)[0] === '2021-05-14');
var expirationSeries = temp != undefined ? Object.values(temp)[0] : 'not found';
console.log(expirationSeries);
Upvotes: 1
Reputation: 123016
You can use Array.find
to find the object within the options array. Something like:
const fromJson = {
stock: {
zip: '95014',
lastUpdated: '05/02/2021'
},
options: [
{ '2021-05-07': {} },
{ '2021-05-14': {} },
{ '2021-05-21': {} },
{ '2021-05-28': {} },
{ '2021-06-04': {} },
{ '2021-06-18': {} },
]
}
const expirationSeries = findOptionByKey(`2021-05-14`);
console.log(expirationSeries);
// not found
console.log(findOptionByKey(`2022-03-12`));
function findOptionByKey(key2Find) {
const keyFinder = obj => obj[key2Find];
return fromJson.options.find(keyFinder) || `option [${key2Find}] not found`;
}
Upvotes: 1
Reputation: 21
Since options is an array you can use find to get the first object that has a property key in it.
const jsonData = {
stock: {
zip: '95014',
lastUpdated: '05/02/2021'
},
options: [
{ '2021-05-07': {} },
{ '2021-05-14': {} },
{ '2021-05-21': {} },
{ '2021-05-28': {} },
{ '2021-06-04': {} },
{ '2021-06-18': {} }
]
}
const key = '2021-05-21';
const result = jsonData.options.find(option => key in option );
console.log(result)
Upvotes: 2
Reputation: 281
You could also try this:
options.forEach(obj => {
const key = Object.keys(obj)[0];
if (key == '2021-05-14') {
expirationSeries = options[key];
};
});
Upvotes: 1