How can you effectively filter a JSON array in JavaScript when the keys include special characters?

I received a JSON response that has the following structure:

[
    {
        "@level": "info",
        "@message": "Text"
    },
    {
        "@level": "error",
        "@message": "Text"
    },
    {
        "@level": "info",
        "@message": "Text"
    }
]

I am trying to figure out how to filter this JSON response so that it only displays Objects where

"@level" === "error"
. I know how to typically filter JSON arrays in JavaScript (for example, as explained here). However, in this case, the solution does not seem to work due to the special character @ in the key.

Can anyone help me understand what I might be overlooking here?

Answer №1

Operates in a similar manner as explained in the linked answer, but make sure to utilize bracket notation with arrays:

let array = [
    {
        "@level": "info",
        "@message": "Text"
    },
    {
        "@level": "error",
        "@message": "Text"
    },
    {
        "@level": "info",
        "@message": "Text"
    },
];

console.log(array.filter((item) => item["@level"] === "error"));

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

The function axios.post(...).then is throwing a TypeError

I'm currently working on a Node.js script that looks like this. const axios = require('axios'); const https = require('https'); const postRequest = (url, data) => { gLogger.debug('postRequest started'); // try { ...

Modify a class attribute within a function

I need to modify the this.bar property within my class when a click event occurs. The issue is that the context of this inside the click function is different from the this of the class. export class Chart { constructor() { this.bar; } showC ...

JQuery jqx validation is experiencing some issues

Utilizing jquery plugins and widgets from jqx for basic form validation in my ruby-on-rails application has proven to be very helpful. Here is a simple example of an HTML form: <form id="newForm"> <input type="text" id="name"/> < ...

What is causing my Mongo database to be unresponsive during my initial request?

In the context of my NodeJS and Mongoose setup, I have two models in my MongoDB: User and Product. My goal is to update a user in both of these models (where a user can have many products and a product has one owner, the user). However, when I make the u ...

The AngularJS filter may encounter issues if the filter's model is located in a separate view that shares the same controller

I am facing an issue with using a filter in my second view while having two views that share the same controller "TabsController as TabsVM". The filter I need to use is "item in items | filter:modelName", where modelName is located in the input field of th ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...

How can I load a .json file into JavaScript without inserting it directly into the code?

Currently, I am dealing with a lengthy JSON snippet that resembles the following: { "section: [ [stuff] ] } To incorporate this into my JavaScript code, I currently use the following approach: var obj = { // {All the stuff from above} } My ...

Angular and Ionic components consistently return clientHeight and clientWidth values of zero

Within my barcode component, I need to ensure that the barcodescanner displays in the correct width and height, matching the dimensions of its container. These specific values are crucial for initializing the scanner accurately. In Angular, I am attemptin ...

Significant increase in response time observed after 3 minutes of inactivity on the Web API

I've encountered a peculiar issue in both my preproduction and production environments (but not in development). My website performs various operations on a Web Api that is hosted on the same IIS server. Typically, a specific POST request is process ...

React for building frontend text sliders

Currently, I am diving into the fundamentals of React and taking on various problem statements found online. One particular challenge tasked me with creating a React app for a text slide generator complete with previous, next, and reset buttons. So far, th ...

The Vue.js integration fails to function within Laravel 5.5's environment

When the + sign is clicked, I want to add a new text field to an existing form. This functionality works fine in my code snippet, but not on my Laravel 5.5 site. No errors are showing up in my console. The HTML code goes into create.blade.php and the Vue ...

Failure to deliver messages through socket.emit

Check out the following JavaScript code snippet: `var express = require('express'); var app = express(); var http = require('http').Server(app); var path = require("path"); var io = require('socket.io')(http); app.get(&apos ...

React - Children components in an array not updating when props are modified within a callback function

The question may be a bit unclear, so let me provide further explanation. This is a personal project I am working on to improve my understanding of React basics and socket.io. Within this project, I have developed a CollapsibleList component and a NestedL ...

Is a switch statement supported by jade's syntax?

When attempting to implement a switch statement in Jade served by Express, I encountered an error message stating "unexpected identifier". - switch(myvar) - case: "0" span First Case break - case: "2" span Second Case ...

I'm testing out using JSON to store a question along with its corresponding answer in a JSON file

I'm currently facing a challenge in saving a question and an answer to a JSON file in a dictionary format. The problem arises when I try to update the existing data with new information. Either the old data gets overwritten or the new data is simply a ...

What is the proper method to trigger a re-render of a React functional component with the useEffect

Within my top-level component, I am utilizing a library to determine if a user’s browser is in light or dark mode. This information is then used to set the theme for the application, which includes HTML Canvas elements (it's crucial to note that the ...

Concentrating on a Div Element in React

I'm trying to set up an onKeyPress event that will be triggered when a key is pressed while a 'Level' element is displayed. I know that the div needs to be focused for events to register, but I'm not sure how to do this with functional ...

Switching background images with Javascript through hovering

I am currently working on implementing a background changer feature from removed after edits into my personal blog, which is only stored on my local computer and not uploaded to the internet. However, I am unsure of what JavaScript code I need to achieve t ...

Post Request to Express API does not produce any output

Today, I encountered an issue while trying to create a JWT authentication API in Express. When I send a POST request with Postman, it only returns {}. Below is the code for server.js: const express = require("express"); const mongoose = require("mongoos ...

Using JavaScript parameters in a HTML document

I am trying to replicate a page similar to this. The issue I am facing is the inability to use external JS files in ASP.net (as far as I know). Therefore, I am defining the functions and attempting to utilize them within the HTML page instead. <%@ P ...