Reputation: 460
I have an array that looks like
const arr = [
{img2: "assets1.png - Copy.png"},
{img2: "lime.jpg"},
{img2: "ginalee.jpg"}
{img2: "cb.jfif"}
{code: "blob:http://localhost:3000/d2c4641a-8586-4bb8-9a14-21b6712856ff", key: "img2", value: "cb.jfif"}
{code: "blob:http://localhost:3000/b406ceb8-92f4-4bbd-9eef-7db7d103b1e3", key: "img2", value: "lime.jpg"}
{code: "blob:http://localhost:3000/28130041-347f-49d9-b30d-72e26c9a6dda", key: "img2", value: "ginalee.jpg"}
{code: "blob:http://localhost:3000/d0d3e9aa-8791-419d-8585-f6c878b161e6", key: "logo", value: ""}
{code: "blob:http://localhost:3000/187977de-6a8f-4815-b3e2-01bfa818bcb7", key: "logo", value: "OIPYYPYPEVF.jpg"}
{code: "blob:http://localhost:3000/dee69b85-6d13-4f81-ba5b-d8db5708d9c8", key: "logo", value: ""}
{code: "blob:http://localhost:3000/676b0366-f30e-4653-9716-ab0ebf4155a2", key: "logo", value: "image (
5).png"}
]
I need to get rid of the ones that say img2 (or really /img[0-9]/) and just about every variation of
arr.filter((f)=>Object.keys(f).includes(...))
isn't working.
any help would be great.
Upvotes: 0
Views: 1120
Reputation: 74
Something like this could work to ignore all img[0-9]:
arr.filter((val) => Object.keys(val).every((key) => !key.match(/^img[0-9]+$/i)));
This will get all the keys for each individual object and evaluate with every
if the key name matches the regex to get the negated value (meaning any value matching the regex will return false
causing .every to stop iterating and return false
to the .filter
function and thus removing those values.
Upvotes: 1
Reputation: 94
for(let i = 0; i < arr.length; i++) {
for(let r in arr[i]) {
if(arr[i][r].match(/(img)[0-9]/ig)) {
arr[i][r] = arr[i][r].replace(/(img)[0-9]/ig, "");
}
}
}
console.log(arr);
Upvotes: 1
Reputation: 370729
Check if .some
of the keys contain img
:
arr.filter(obj => Object.keys(obj).some(key => !key.includes('img')))
Upvotes: 2