trying to check if a value of array present on a base array and return as string-
base array:
const filters = {
"name: "location",
"data" : [
"dana-point",
"canada-so"
]
}
const posts = [
{
"date": "09 February 2022",
"title": "There are many variations of passages of Lorem Ipsum available",
"locations": [
"dana-point",
"new-york"
],
"url": "/news/many-variations",
},
{
// so may items
},
]
I wanted to return all post which matched with filter data object with at least one value match like - on above example it will retrun first post as its match the location, dana-point.
let filteredPosts = posts.filter(item => checks.includes(item[checks.data])));
it's not working
You can do something like this:
const checks = [
{
name: "locations",
data: ["dana-point", "canada-so"]
}
];
const posts = [
{
date: "09 February 2022",
title: "There are many variations of passages of Lorem Ipsum available",
locations: ["dana-point", "new-york"],
url: "/news/many-variations"
}
];
const filtered = posts.filter((post) =>
checks.every((check) =>
post[check.name].some((data) => check.data.includes(data))
)
);