Currently, I have an object with keys that have multiple values.
Additionally, there is an array with a simple key check as follows: ["random", "three"]
.
My goal is to retrieve the mainData, but only with the object and the data that match the keys in the array, i.e. ["random", "three"]
.
Existing Code:
const mainData = {
random: {
name: "Random Entity",
date: "05/04/2022",
startTime: "19:00",
finishTime: "00:00",
},
one: {
name: "One Entity",
date: "16/04/2022",
startTime: "16:00",
finishTime: "20:00",
},
three: {
name: "Three Entity",
date: "19/04/2022",
startTime: "10:00",
finishTime: "11:00",
},
};
export default mainData;
Desired Output:
const mainData = {
random: {
name: "Random Entity",
date: "05/04/2022",
startTime: "19:00",
finishTime: "00:00",
},
three: {
name: "Three Entity",
date: "19/04/2022",
startTime: "10:00",
finishTime: "11:00",
},
};
Attempted Solution:
let filterKey = 'random';
const result = Object.entries(mainData).filter(([k, v]) => k == filterKey);
This solution only works for a single search, not for searching through an array.