Javascript: searching for a key to return a value

javascript object get value by key
javascript get value by key in array
javascript array of objects get value by key
javascript object keys values
javascript find
javascript compare object key values
javascript map key, value
javascript object values

Hi am doing a in javascript: The data will be map. The key will be the search target. If you find that target in your data, return the value of that word and show it on your page.

I cannot get it search functionality to work. Like, if I want search for Harry, it should give me result "an old English word".

Any ideas for this to fix?

function myfunction() {
  var data = new Map();
  data.set("Harry", "something.");
  data.set("Corhoo", "corn.");
  data.set("Elephant", "big ");

  var keys = Object.keys(data);

  //Need help with this loop, to get the search work
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (keys[i] == "Harry") {
      document.getElementById("result").innerHTML = data.get("Harry");
    }
    //console.log(data[keys[i]]);
  }
}
Enter: <input type="text" />
<p id="result"></p>
<p id="result2"></p>
<input type="button" value="SearchIt" onclick="myfunction()" />

Why you want to use loop? It's better to use Map.proptype.has method:

Enter: <input type="text" id="key"/>
<p id="result"></p>
<p id="result2"></p>
<input type="button" value="SearchIt" onclick="search()" />
var data = new Map();
data.set("Harry", "An old English word that means Potter.");
data.set("Corhoo", "The Gaelic version of America Cornhole.");
data.set("Elephant", "A largest plant resembling an Ele");

function search(key) {

  var key = document.getElementById('key').value;
  var result = document.getElementById("result");

  if(data.has(key)) {
      result.innerHTML = data.get(key);
  } else {
      result.innerHTML = "No key found";
  }
}

How to get a key in a JavaScript object by its value?, indexOf(value); // Finding value index return kArray[vIndex]; // Returning key by value index }. Object with keys and values: var greetings = { english : "hello",� The find () method is used to return the value of the first element that satisfies the testing function. If the value matches, then this condition is satisfied and the respective key is returned. This is the key to the value of the object. Note: This method was added in the ES6 specification and may not be supported on older browser versions.

If you do console.log of keys from var keys = Object.keys(data); it will give an empty array. You may not need to do Object.keys

function myfunction() {
  var data = new Map();
  data.set("Harry", "An old English word that means Potter.");
  data.set("Corhoo", "The Gaelic version of America Cornhole.");
  data.set("Elephant", "A largest plant resembling an Ele");
  if (data.has('Harry')) {
    document.getElementById("result").innerHTML = data.get("Harry");
  }
}
Enter: <input type="text" />
<p id="result"></p>

<p id="result2"></p>

<input type="button" value="SearchIt" onclick="myfunction();" />

How to get a key in a JavaScript object by its value ?, The find() method is used to return the value of the first element that satisfies the testing function. If the value matches, then this condition is satisfied and the respective key is returned. This is the key to the value of the object. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs. Please note the distinctions (compared to map for example):

Maybe I'm misunderstanding the question, but this should work to map out the keys and return the value if the key is found.

const data = {
  'Harry': 'An old English word'
}

Object.keys(data).map(key => {
  if (key === 'Harry') {
    return data[key]
  }
}

ETA:

If using Map to create key-value pairs, use this instead:

data.forEach(item => {
  if(item[0] === 'Harry') {
    document.getElementById("result").innerHTML = item[1]
    // or whatever you want to run if 'Harry' is found
  }
}

For searching a particular term:

const searchTerm = 'Harry'

data.forEach(item => {
  if(item[0] === searchTerm) {
    document.getElementById("result").innerHTML = item[1]
    // or whatever you want to run if 'Harry' is found
  }
}

Object.values(), The Object.values() method returns an array of a given object's own the values are returned in the keys' numerical order const arrayLikeObj2� In PEG.js I have the following rule label = l:[a-zA-Z\\$\\#\\% ]* { return word(l); } block = "[" l:label "]" { return l; } option = "\ "* key:block value:label "\

Object.keys(), Syntax. Object.keys( obj ). Parameters. obj: The object of which the enumerable's own properties are to be returned. Return value. An array of� var myArray = [{key: "key1", value: "value1"},{key: "key1", value: "value1"}]; Is there an elegant mechanism for determining if "key1" is present ? In other words, a better mechanism than iterating through all array elements and checking each one.

JavaScript: manipulating objects with Object.keys() – clubmate.fi, The Object.keys() method returns an array of a given object's own Array. prototype.some() method makes it easy to check if some values in the object pass and helpful posts I've run across while searching to learn the JS Object property. Object.keys(obj) Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object. Description. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object

JavaScript Array indexOf() Method, Negative values will start at the given position counting from the end, and search to the end. Technical Details. Return Value: A Number, representing the position � Are you looking for an array of objects that match on a particular value? Perhaps you want both, grabbing the object that matches a key and value. The getObjects function below will allow you to retrieve all these things from your JSON string. I have also included a getValues function that will return an array of values that match on a certain key.

Comments
  • What I a trying to do, it when I search any of the three keys, using if statement, it should only print out, the matching value of key. That is not working for me. So, if harry, print "An old English work, else if Corhoo, print "The Gaelic version", else if Elephant, print"A largest plant. --- When I put all three if statements, it prints all values, even when I search Harry
  • @PTL, take a look at my edit. Is that you are looking for?
  • yes, this is exactly what I was trying to do. Tried all different ways, but wasn't getting the right way. Thank you very much for you help.
  • Just FYI, if this worked best for you, you should mark it as the answer so others that find this thread know which one worked. I'm giving it an upvote for its elegance among other reasons.
  • Yes, this work. But let say, I want to search Corhoo, and I put another if statement similar to harry to search for Corhoo, it doesn't work. it prints both of the values.
  • It wouldn't give me the result. My original questions is: Create a page that does a search through your data to find the meaning of that word. The data will be map. They key will be the search target. If I find that target in my data, return the value of that word and show. So, if I search Harry, show value of Harry, if I search Corhoo- show value of that.
  • I'm not sure if you get a notification for an edit on an answer, but the update should answer your question using your Map data
  • Thank you very much!!