I have this JavaScript object:
const DayOfWeek = [
{ key: 'MON', value: 'M' },
{ key: 'TUE', value: 'T' },
{ key: 'WED', value: 'W' },
{ key: 'THU', value: 'T' },
{ key: 'FRI', value: 'F' },
{ key: 'SAT', value: 'S' },
{ key: 'SUN', value: 'S' },
];
Note: If frequencyDayOfWeek is WED replace with object { key: 'WED', value: 'W' }
let frequencyDayOfWeek = [
"WED",
"THU",
"FRI",
"SAT"
];
frequencyDayOfWeek.map((day, index) => { //not working
day.find((data, i) => {
if (DayOfWeek[i].key == data) {
//return match arrays of object;
// [{ key: 'MON', value: 'M' }]
// [{ key: 'WED', value: 'W' }]
}
})
});
So here is the result I want to get:
"frequencyDayOfWeek": [
{ key: 'WED', value: 'W' },
{ key: 'THU', value: 'T' },
{ key: 'FRI', value: 'F' },
{ key: 'SAT', value: 'S' },
]
How can I implement it using JavaScript?
Heretic Monkey
12.2k7 gold badges63 silver badges133 bronze badges
-
Does this answer your question? How to filter array when object key value is in arrayHeretic Monkey– Heretic Monkey2021年06月08日 12:28:35 +00:00Commented Jun 8, 2021 at 12:28
2 Answers 2
You need to return a value from the mapping function.
For example:
const DayOfWeek = [
{ key: 'MON', value: 'M' },
{ key: 'TUE', value: 'T' },
{ key: 'WED', value: 'W' },
{ key: 'THU', value: 'T' },
{ key: 'FRI', value: 'F' },
{ key: 'SAT', value: 'S' },
{ key: 'SUN', value: 'S' },
];
let frequencyDayOfWeek = [
"WED",
"THU",
"FRI",
"SAT"
];
const out = frequencyDayOfWeek.map(k => {
// actually return a value
return DayOfWeek.find(x => x.key === k);
});
// or short:
// const out = frequencyDayOfWeek.map(k => DayOfWeek.find(x => x.key === k));
console.log(out);
answered Jun 8, 2021 at 12:22
Yoshi
54.8k14 gold badges93 silver badges108 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Yoshi
@HereticMonkey Well, link a duplicate. The one you linked concerns filtering, which is not what this question is about. It's about mapping one structure using data from another. Obviously the same could be achieved with filtering (in this specific context). But ultimately that's not the problem here. The problem is that they need to return a value from map/find or filter (if they so choose).
Heretic Monkey
A distinction without a difference. There is no difference between the output of your
map and the output of filter. Both arrays. Both point to the original objects.Heretic Monkey
And, I'm guessing you didn't even look for a duplicate before answering.
Yoshi
@HereticMonkey You're right for this specific case, though we don't know if
frequencyDayOfWeek always is a subset of DayOfWeek it might be a longer stream. In which case filtering DayOfWeek would yield the wrong result.Heretic Monkey
There are infinite ways in which both
filter and map could fail. Your code will only return the first object with the key that matches; if there are two, then the second one will never get selected. So what? We can only answer the question as stated by the OP. And, as you've just admitted, this specific question is a duplicate of the mentioned question (which has answers that use map).frequencyDayOfWeek.map(f => DayOfWeek.find(d => d.key === f));
sawan
2,3813 gold badges26 silver badges52 bronze badges
Comments
lang-js