0

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
  • 11,687
  • 7
  • 53
  • 122
md has
  • 35
  • 4
  • Does this answer your question? [How to filter array when object key value is in array](https://stackoverflow.com/questions/35817565/how-to-filter-array-when-object-key-value-is-in-array) – Heretic Monkey Jun 08 '21 at 12:28

2 Answers2

1

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);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • @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). – Yoshi Jun 08 '21 at 12:31
  • 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 Jun 08 '21 at 12:39
  • And, I'm guessing you didn't even look for a duplicate before answering. – Heretic Monkey Jun 08 '21 at 12:41
  • @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. – Yoshi Jun 08 '21 at 12:41
  • 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`). – Heretic Monkey Jun 08 '21 at 12:52
0
frequencyDayOfWeek.map(f => DayOfWeek.find(d => d.key === f));
sawan
  • 2,341
  • 3
  • 25
  • 51