Choose from an array of JSON objects

I have an example array below:

{
 "orders": [
   {
     "order_id": 1,
     "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="345558514c745558514c1a575b59">[email protected]</a>",
     "city": "London"
   },
   {
     "order_id": 2,
     "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6873715c687371327f7371">[email protected]</a>",
     "city": "Miami"
   }
 ]
}

Along with a variable

var email = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1662797b5662797b3875797b">[email protected]</a>"
. I need to
retrieve order_id and city from the array where email is equal to "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c4dfddf0c4dfdd9ed3dfdd">[email protected]</a>"
Do you have any suggestions on how to accomplish this?

Answer №1

Utilizing the power of .filter() and .map():

const data = { "orders": [{ "order_id": 1, "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06676a637e46676a637e2865696b">[email protected]</a>", "city": "London" }, { "order_id": 2, "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70041f1d30041f1d5e131f1d">[email protected]</a>", "city": "Miami" }] };

const result = data.orders.filter(({email}) => email === '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bffe4e6cbffe4e6a5e8e4e6">[email protected]</a>')
                          .map(({order_id, city}) => ({ order_id, city }));

console.log(result);

Answer №2

Check out the code snippet:

const data = { 
"users": [
   {
     "user_id": 1,
     "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5e535a476f5e535a46305d5153">[email protected]</a>",
     "city": "New York"
   },
   {
     "user_id": 2,
     "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0cacecbc3c7b0d6cac2739edbcacbbcfc2cecc">[email protected]</a>",
     "city": "Los Angeles"
   }
 ]
}
var userEmail = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="48382139003821463b3735">[email protected]</a>"
data['users'].map(function(user){
if (user.email === userEmail){
  console.log(user.user_id);
  console.log(user.city);
}
});

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

transforming JSON information within an angularJS framework into an array and displaying it visually with a C3 chart

Looking to create an x-y plot using AngularJS with a JSON data feed. The JSON data displays EQ magnitude VS time. How do I convert this data into an array format and plot it in a c3 chart? (similar to the one in this link ) Appreciate any assistance you c ...

Having trouble retrieving JSON data from an external source

I'm currently facing a challenge where I am unable to fetch JSON data from a webpage despite multiple attempts. I keep encountering the following errors: Uncaught SyntaxError: Unexpected token : or XMLHttpRequest cannot load URL. No 'Access-Co ...

Having trouble with removing the disabled attribute from an input file submit button

Currently, I am in the process of validating the file size and extension before it gets uploaded. My code is almost functioning perfectly, but there seems to be an issue with removing the disabled attribute from the submit button when the file meets all th ...

Retrieve the calculated events of an HTML element using JavaScript

Is there a way to retrieve the computed click events from an HTML object using JavaScript? I want to access the functions that are triggered when clicking on an HTML element. Is there a method to directly obtain the function calls instead of using the s ...

What is the best way to send a variable from my controller to the HTML and then to a JavaScript function?

After my controller runs its course, it finally outputs the following: res.render('my-html', { title: `${title}`, myVar1, myVar2 }); Once this data is rendered in the HTML, I am able to directly display it. However, my goal is to then pass thes ...

What is the process for invoking a function using a directive's service in Angular?

manager angular.module('myApp') .controller('myCtrl', [ '$mdDialog', '$mdMedia', 'viewDialog', myCtrl]); .. function myCtrl( $mdDialog, $mdMedia, viewDialog) { $scope.openView = function(ev,id) ...

What is the best way to divide the dev-server.js file in Vue into multiple separate files?

Currently, I am utilizing vue.js to develop an application and have created a mock login api on localhost in dev-server.js. Now, I am looking to refactor the code related to the login api into a separate file. Do you have any suggestions on how I can ach ...

Generate a two-dimensional array of pixel images using HTML5 canvas

Hey everyone, I'm trying to copy an image pixel to a matrix in JavaScript so I can use it later. Can someone take a look and let me know if I'm using the matrix correctly? I'm new to coding so any help is appreciated. Thanks! <canvas id= ...

Error message: Unable to iterate through a non-iterable object in React reducer

I find myself in a unique predicament and could use some assistance. userData : { isValidCheckup: true, accounts: { userAccount: [ { accountType: 'checkings', includeInCheckup: false }, { accountType: 'check ...

Changing the default right-click functionality on a website with JavaScript

When using a web application, the default behavior is to display a context menu when right-clicking. However, in my case, I want to replace this default context menu with a custom one. Despite writing code to achieve this, both menus are currently being di ...

Incorporate New Element into the Express Request Object

I am attempting to enhance the Express request object by adding a property called "forwardingUrl". To achieve this, I utilized declaration merging in a file named ./typing.d.ts: declare namespace Express { export interface Request { forwardingUrl: ...

Troubleshoot: React and Laravel login authentication issues

I am relatively new to working with React, React Router, and Laravel for authentication. I've been trying to set up authentication using Laravel, React, and React Router, and although the page redirects to the desired destination when the submit butto ...

Using React Router useHistory to navigate to different routes programmatically

Currently, I'm working on creating a wizard interface and running into some issues with the buttons. Right now, I have next and back buttons for each route, but I am aiming to create a button component that can handle navigation both forward and backw ...

What is the best way to extract JSON data from a SOAP web service response and store it in a dictionary on an iOS device?

Currently, I am utilizing a SOAP web service. After making the request to the web service, I have received the response below: [{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}]<?xml version="1.0" encodin ...

Using ES6 to create a callback function that stores values in a Map

I've been trying to find an answer to this question by searching extensively, but haven't had any luck. Currently, I am developing a React app and faced with the task of storing the state, which happens to be a map, in a local variable. Here is ...

How to Transfer Information Between Two React Components

I recently started learning React and I'm not sure how to approach this task. Basically, I have a list of cars and when the user clicks on each car, it should display detailed information about that specific car in a full-page slide. This is my curr ...

Mastering jQuery placement using CSS3 "transform: scale"

There seems to be an issue with jQuery not functioning well with CSS "transform: scale()" (however, it works fine with "transform: translate()") Please examine this straightforward example: $(document).ready(function() { $('#root').dblclic ...

Is there a way to conceal the headers during an ajax request?

I'm currently exploring APIs using jQuery and Ajax. To set the header of Ajax, I typically use code similar to this: headers: { "My-First-Header":"first value", "My-Second-Header":"second value" }, I am working on a basic school project and ...

In JavaScript/jQuery, there is a technique for retrieving the values of dynamically generated td attributes and the id tags of elements inside them within tr

I am currently working on creating a calendar of events using PHP, jQuery, and ajax. The calendar is embedded within an HTML table, where the rows and fields are dynamically generated based on the number of days in a specific month. In order to successfull ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...