What is the process of accessing a Key-Value object in Java through a query?

If I have a list of strings, how can I search for all values in a key-value object using my list of strings, and then return the corresponding keys?

"listOfStrings": ["4444", "5555"]

"secretCodes": [
{
"secret": "John",
"value": "4444"
},
{
"secret": "Paul",
"value": "0001"
},
{
"secret": "George",
"value": "0002"
},
{
"secret": "Ringo",
"value": "5555"
},
{
"secret": "Pete",
"value": "0008"
}
]

Answer №1

If you're looking to retrieve the secret aliases that correspond with the codes given in the array, you can achieve it using the following method:

let secretCodes = [
  { secret: "John", value: "4444" },
  { secret: "Paul", value: "0001" },
  { secret: "George", value: "0002" },
  { secret: "Ringo", value: "5555" },
  { secret: "Pete", value: "0008" },
];

function getSecretName(codes) {
  let matchingValues = secretCodes.filter(obj => codes.includes(obj.value));
  return matchingValues.map(value => value.secret);
}

console.log(getSecretName(["4444", "5555"]));

// Output: ["John", "Ringo"]

Answer №2

Iterate over the elements and save them in a designated variable.

const classifiedItems = [];

confidentialData.forEach(element => {
  if (Sensitive.includes(element.value)) {
    classifiedItems.push(element)
  }
})

Answer №3

To effectively handle data of this nature, utilizing the Array prototype functions is recommended.

  • filter works well when you need an array that contains a specific subset of the data
  • map is ideal for transforming each element into a new one
  • some is most suitable when determining if any element in the array matches your criteria

SecurityCodes = ["4444", "5555"]

secretCodes = [
{"secret": "John", "value": "4444"},
{"secret": "Paul", "value": "0001"},
{"secret": "George", "value": "0002"},
{"secret": "Ringo", "value": "5555"},
{"secret": "Pete", "value": "0008"}
]

const validCodes = secretCodes.filter(code => SecurityCodes.some( el => el === code.value))
console.log(validCodes)

const approvedSecrets = validCodes.map(code => code.secret)
console.log(approvedSecrets)

Answer №4

When you assign the variables `secretCodes` and `codes`, you can use this code snippet:

secretCodes.forEach(item =>{
        codes.forEach(code =>{
            if (item['value'] === code){
                console.log(item['secret'])
            }
        })
    })

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

Retrieve a specific key value from a dictionary within a Flask function by employing JavaScript

I'm currently working on a feature where a user can input something in a search field, and upon submitting, the script should send a request to a Flask function using JavaScript. The response data should then be loaded accordingly. However, I've ...

Is there a way to utilize JSON.NET to deserialize a collection of JSON objects into an array?

The terms "array" and "objects" might not be exact, but you catch my drift. Looking at the sample for serializing/deserializing a custom object from the official documentation: product.Name = "Apple"; product.ExpiryDate = new DateTime(2008, 12, 28); p ...

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

Stop :hovering on the link for iPad

My webpage contains sublinks with CSS properties as follows: #leftNav .searchBySub {...} #leftNav a.searchBySub:hover {...} #leftNav .searchBySubClicked {...} However, I have observed that on the iPad, the :hover styles are being applied. Is there a wa ...

Utilizing Jquery on the client side in conjunction with a Node.js server

I am using nodejs within a docker-compose container to create a local web app. My goal is to use jquery on the client-side to load an HTML file into a div, but I'm encountering issues with it not working properly. Both the initial index.html and the n ...

Modify cells in a table by applying conditions with the help of an array

In my workbook, I have a dynamic master table located on Worksheets("Jobs") with a fixed number of columns (A:M). Columns I and J contain formulas. https://i.sstatic.net/yx7rE.jpg On a separate worksheet, there is a textbox where a job number is entered. ...

Issue encountered while attempting to deactivate button until numerical data is entered in the OTP field using Vuejs

otp_value: '', isFadeout: false, verifyOtp() { this.disabled = true; this.otpBtnClicked = false; this.verified = true; }, <input class="o ...

Why is there a 'SyntaxError: Unexpected token e in JSON at position 0' error appearing exclusively on Chrome?

Today, I came across an unusual error that seems to only occur when loading localhost:9000 on Chrome! Interestingly, http://parke.linkpc.net:9000/ works perfectly fine on Chrome. On the other hand, localhost:9000 runs smoothly on Firefox. Even after going ...

Modifying the JSON output of a web API

After creating an API in visual studio 2015, I noticed that while running the API, it provides me with the expected response and data. Below you can find my controller code: public HttpResponseMessage GetByMsn(string msn, DateTime dt) { try ...

Utilizing computed properties to implement table filtering in Vue.js

I am currently working on a table generated from an array of objects in VuE.js and struggling with implementing computed properties for filtering. I need help figuring out the correct way to use filter() within my computed properties to filter the table ba ...

Display well-formatted JSONs with a mix of string keys

I am facing a situation where I have a Redis hash containing keys and values in a specific format. When I run a query (hgetall some_redis_hash) in rediscli, the output is dumped into a file: redis_key1 {"value1__key1": "value1__value1", ...

I am having trouble getting my Jquery to locate the panelid

I tried following a tutorial to improve my coding skills, but I'm facing issues with my code. It seems like my jQuery isn't able to locate the panelid. I can successfully alert the panelids, but the functionality of clicking on "läs mer" and dis ...

Conceals portions of the elements in an array

Could you please guide me in identifying the issue with my code? I am currently attempting to test values within an array. Each value in the array represents 16 bits (flags), and my objective is to determine whether the flag is set or not. Below is the o ...

Are specific names set aside for JQuery Selectors?

One time, I made a mistake with my selector: $("#cut") Unfortunately, this was not pulling the object correctly even though I had defined an object with that id in the HTML. After changing the object's id (and updating the selector), everything work ...

Is it possible for JavaScript to detect elements or scripts within a hidden element using display="none"?

Is it possible for scripts to locate elements that have been hidden in the DOM by setting their attribute to display="none"? ...

Currently experimenting with implementing malloc functionality in C through functions

Hi there! I'm currently trying to understand why Valgrind is throwing an invalid write of size error at the line: array[i-1] = I; I can't seem to figure out what's wrong with my allocate_array function. I've tried multiple approaches b ...

Executing various count() queries on a MongoDB using mongoose

Hey there, I consider myself a MongoNoob and I know this question has probably been asked before in various ways, but I haven't found a specific solution yet. Let's imagine I have two Moongoose Models set up like this: var pollSchema = mongoose. ...

Avoid the default behavior when employing jQuery for Ajax requests

I am facing an issue with preventing the default action when submitting a form using ajax. It seems that my code is causing a page refresh and clearing the form without sending any data to the database. I tried including the php processing script link in t ...

How can I find out the direction in which my mesh is facing using three.js?

I am currently working on a project to develop a simple game that involves shooting projectiles out of a gun barrel. At the moment, I have a basic setup where a cube and a cylinder are combined as the barrel. When I rotate the group containing these elemen ...

FullCalendar experiencing issues displaying some events when using AJAX/JSON

I've been experimenting with FullCalendar for some time now, but I'm facing a minor issue. When I load events through AJAX/JSON in the correct format, the calendar only displays the first event. Although all other events are visible in the conso ...