Search code examples
javascriptarraysfilter

Filter array by Object key


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.


Solution

  • Check if .some of the keys contain img:

    arr.filter(obj => Object.keys(obj).some(key => !key.includes('img')))