问题
I have objects like this one:
{
"name": "Ola",
"dates": [
{
"7.01.2020": [1, 2, 3]
},
{
"8.01.2020": [4, 5, 6]
},
{
"9.01.2020": [7, 8, 9]
}
],
"id": 7
}
and I need to filter through dates object to check if there is specific date and return it's values (ex. if there is 7.01.2020). When I try fro ex user.dates, I get array with 3 different objects but when I use filteror map method, it doesn't work.
When user pick date I need to check if this date exists and add task to the existing ones and if it's new date, add date with tasks...
Any ideas? Thanks!
回答1:
Actually you need to use for loop if it's not working with filter and map. I would suggest you to access date according to keys from the above arrays
let x = {
"name": "Ola",
"dates": [
{
"7.01.2020": [1, 2, 3]
},
{
"8.01.2020": [4, 5, 6]
},
{
"9.01.2020": [7, 8, 9]
}
],
"id": 7
}
let y = Object.values(x.dates);
for(const [key,value] of Object.entries(y))
{
//getting particular date string
result = Object.keys(value);
console.log(result);
//converted date for result
date = new Date(result);
console.log(date);
}
回答2:
I'd probably use a nested loop, where the inner loop is over the properties of the object:
let result = null;
outer: for (const entry of obj.dates) {
for (const [key, value] of Object.entries(entry)) {
if (key === target) {
result = value;
break outer;
}
}
}
Live Example:
const obj = {
"name": "Ola",
"dates": [
{
"7.01.2020": [1, 2, 3]
},
{
"8.01.2020": [4, 5, 6]
},
{
"9.01.2020": [7, 8, 9]
}
],
"id": 7
};
const target = "7.01.2020";
let result = null;
outer: for (const entry of obj.dates) {
for (const [key, value] of Object.entries(entry)) {
if (key === target) {
result = value;
break outer;
}
}
}
console.log(result);
That makes at most one pass through the data, stopping as soon as it finds a match.
It can be a non-nested loop if you know that the objects in obj.dates only have a single property:
let result = null;
for (const entry of obj.dates) {
const [key] = Object.keys(entry);
if (key === target) {
result = value;
break;
}
}
Live Example:
const obj = {
"name": "Ola",
"dates": [
{
"7.01.2020": [1, 2, 3]
},
{
"8.01.2020": [4, 5, 6]
},
{
"9.01.2020": [7, 8, 9]
}
],
"id": 7
};
const target = "7.01.2020";
let result = null;
for (const entry of obj.dates) {
const [key] = Object.keys(entry);
if (key === target) {
result = entry[key];
break;
}
}
console.log(result);
Another option would be to turn obj.dates into a Map and then use get on it:
const map = new Map(obj.dates
.map(entry => {
const [key] = Object.keys(entry);
return [key, entry[key]];
})
);
const result = map.get(target);
Live Example:
const obj = {
"name": "Ola",
"dates": [
{
"7.01.2020": [1, 2, 3]
},
{
"8.01.2020": [4, 5, 6]
},
{
"9.01.2020": [7, 8, 9]
}
],
"id": 7
};
const target = "7.01.2020";
const map = new Map(obj.dates
.map(entry => {
const [key] = Object.keys(entry);
return [key, entry[key]];
})
);
const result = map.get(target);
console.log(result);
That makes a full pass through the data, then a hashed (or similar) lookup on the resulting Map. It's useful if you need to look for multiple targets (since you just build the Map once). Note that this version assumes the objects in obj.dates only have one property.
来源:https://stackoverflow.com/questions/59625319/how-to-filter-objects-with-different-keys-in-array