Filter an array based on the key of an object

I am faced with an array structured like this

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 am seeking a way to eliminate the entries containing img2 (or any variation of /img[0-9]/) using something similar to the code

arr.filter((f)=>Object.keys(f).includes(...))
, but it's not yielding the desired result.

Any assistance on resolving this issue would be highly appreciated.

Answer №1

Verify whether any of the keys include the term img:

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

Answer №2

Iterating through the elements of an array using nested loops, removing any strings that match a specific pattern related to images. Once the modifications are done, the updated array is printed to the console.

Answer №3

Here is a method that can be used to exclude all img[0-9] elements:

arr.filter((val) => Object.keys(val).every((key) => !key.match(/^img[0-9]+$/i)));

This code snippet loops through each object in the array, checks if any key matches the regex pattern /^img[0-9]+$/i, and filters out those objects with keys that match, resulting in only non-matching objects remaining in the array.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to encode only a specific section of a JavaScript object into JSON format?

Currently, I am in the process of developing a 2D gravity simulation game and I am faced with the challenge of implementing save/load functionality. The game involves storing all current planets in an array format. Each planet is depicted by a Body object ...

The setInterval() function causing unusual overlapping issues

While experimenting with creating a basic text carousel, I encountered a puzzling issue that has me stumped. The structure of the carousel is straightforward. It consists of a wrapper and the actual text to be cycled through. Unfortunately, the problem a ...

Expanding application size by incorporating third-party libraries in NPM and JavaScript

My friend and I are collaborating on a project. When it comes to programming, he definitely has more experience than I do, considering I've only been coding for a little over a year. I've noticed that he prefers building components and function ...

Mismatch between MIME types in Bootstrap and Scroll-out detected

I am currently working on a WordPress website and encountering two issues with loading bootstrap.js and ScrollOut.js: - Blocked resources due to MIME type mismatch wp_enqueue_style('bootstrapjs', 'https://stackpath.bootstrapcdn.com/bootstra ...

How come querySelector is effective while $() or document.getElementById do not respond properly with checkboxes?

Lynda.com's "Jquery Essential Training" course includes an assignment to create checkboxes that show and hide items with a specific data attribute based on whether they are checked or not. document.querySelector('#vitamincheck').addEventLis ...

What could be the reason for receiving the error message "Unresolved variable or type approvedPriceDealerCostForm" in my HTML

I'm working with Angular 8 and facing an issue with sending the approvedPriceDealerCostForm when clicking my button. I keep getting an error that says "Unresolved variable or type approvedPriceDealerCostForm". Please refer to the attached image for mo ...

I found that using Enzyme's shallow().text() in React Native did not perform as I had anticipated

I'm currently exploring React Native testing using enzyme and react-native-mock. Excluded from the discussion: A customized compiler for mocha to utilize Babel features. Here is the code snippet: Block.jsx: import React from 'react'; imp ...

What is causing my axios request to not retrieve the correct data?

My axios instance is set up to connect to an API that has a login route. When I test the API using Postman, everything works perfectly and it returns JWT access and refresh tokens for valid credentials. However, when I try to login through my app using axi ...

What steps can be taken to fix the issue of 'this is not defined' when trying to extend EventEmitter?

The code snippet below is encountering an error: var EventEmitter = require('events'); class Foo extends EventEmitter{ constructor(){ this.name = 'foo'; } print(){ this.name = 'hello'; co ...

How can I inform the View/Component in React+Flux that an action has failed?

I am currently working on a Registration Form Component, where the form triggers a createUser action upon submission. This action uses an ajax api call to create a new user. If the user already exists, the action fails. Since we know we cannot return a res ...

Searching within a container using jQuery's `find` method can sometimes cause jQuery to lose control

I am trying to extract information from an input field within a table in a specific row. Here is the code I am using: var myElements = $('#myTable tbody').find('tr'); console.log(myElements); This correctly displays the items in the ...

How can we make shadowing take center stage?

I have a design with two stacked divs, and I'm trying to create the illusion that one is behind the other using shadow effects. When I add a shadow to the left side of the right div, it works perfectly. However, when I try to add a shadow to the right ...

What is the method for retrieving a specific item within an array object using its index number?

How can I access the object '2016-11-12' inside the $scope.list and display the Order and Balance values? List: $scope.list = { ItemCode:'item1', '2016-11-12':[{Order:'1',Balanace:'2'}], '2016- ...

What is the method for binding context on a click event in Vue?

Can't access context function on click in Vue Example HTML with click function: <li v-on:click="itemClick($event, this)"></li> My Vue.js code snippet: var app = new Vue({ el: '#mainApp', methods: { ite ...

Obtain the date in the following format: 2016-01-01T00:00:00.000-00:00

Can someone help me convert this date to the correct format for the mercadolibre api? I need it to be like this: 2016-01-01T00:00:00.000-00:00 However, when I try with the following code: var date_from = new Date(); date_from.setDate(date_from.getDa ...

What is the best way to eliminate "?" from the URL while transferring data through the link component in next.js?

One method I am utilizing to pass data through link components looks like this: <div> {data.map((myData) => ( <h2> <Link href={{ pathname: `/${myData.title}`, query: { ...

Transfer the temporary character array from the for loop to a separate array

I am currently working on a function that takes a constant char * data as input, processes each byte through some bit manipulation, and then adds it to another char array. The goal is to convert one byte of input data into 4 bytes, 2 bytes into 8 bytes, an ...

Tips for displaying a restricted quantity of items in a list according to the number of li lines used

My approach involves using a bulleted list to display a limited number of items without a scrollbar in 'UL' upon page load. On clicking a 'more' button, I aim to reveal the remaining items with a scrollbar in UL. The following code acco ...

Troubleshooting code: JavaScript not functioning properly with CSS

I am attempting to create a vertical header using JavaScript, CSS, and HTML. However, I am facing an issue with the header height not dynamically adjusting. I believe there might be an error in how I am calling JSS. Code : <style> table, tr, td, t ...

Engaging three-dimensional design inspired by JavaScript frameworks

I'm interested in developing an interactive 3D application using JavaScript. I'm curious to know if either babylon.js or three.js have support for interactivity. I've searched for information on this but haven't found much, and the docu ...