0
        var dataHolder = [
        {
            "letterA" : "Fruits",
            "letterB" : "Veges",
            "letterC" : "Meat"
        }
    ];
    console.log(dataHolder[0].letterA);
    var result = "";
    function getData(myLetter) {
        for (var i = 0; i < dataHolder.length; i++) {
           if(dataHolder[i][myLetter] === myLetter){
               console.log(dataHolder[i][myLetter]);
           }
           else{
               console.log("No data found");
           }
            }
        }
    getData("letterA");

This is my code and i'm just trying to match the content of the array with the passed parameter, but every time it's giving No data found as output and not the matching content, it seems i'm missing something very basic here. Any help would be highly appreciated.Thanks!!

2
  • You are comparing the value in the key. For example if you call getData("letterA") the comparison will be between Fruits and letterA. Commented Jul 6, 2017 at 11:20
  • It is a json object use json functions like json.hasOwnProperty(key) to achieve your goal. Commented Jul 6, 2017 at 11:25

2 Answers 2

2

You matching was wrong. you are matching the letters == fruites .You should check is the key exist or not ,that's enough using hasOwnProperty()

Check this below. i was mention the error

var dataHolder = [{
  "letterA": "Fruits",
  "letterB": "Veges",
  "letterC": "Meat"
}];

var result = "";

function getData(myLetter) {
  for (var i = 0; i < dataHolder.length; i++) {
  console.log('this is the pblm  '+dataHolder[i][myLetter] +' != '+myLetter)
     if (dataHolder[i].hasOwnProperty(myLetter)) {
      console.log(dataHolder[i][myLetter]);
    } else {
      console.log("No data found");
    }
  }
}
getData("letterA");

For your way use with for...in

var dataHolder = [{
  "letterA": "Fruits",
  "letterB": "Veges",
  "letterC": "Meat"
}];

var result = "";

function getData(myLetter) {
  for (var i in dataHolder) {
    if (dataHolder[i].hasOwnProperty(myLetter)) {
      console.log(dataHolder[i][myLetter]);
    } else {
      console.log("No data found");
    }
  }
}
getData("letterA")

Sign up to request clarification or add additional context in comments.

3 Comments

This works as long as the value of dataHolder[i][myLetter] is truthy. I'd probably tweak it a bit to use either the in operator o the hasOwnProperty method.
myLetter in dataHolder[i]
With this change you can find values like null, undefined, "", which where previously considered "no data" even when property existed. Okay, but not sure for what this can be useful.
1

You are comparing value with key that is wrong.

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as own (not inherited) property.

Use hasOwnProperty to check key exists or not.

dataHolder[i].hasOwnProperty(myLetter)

var dataHolder = [
        {
            "letterA" : "Fruits",
            "letterB" : "Veges",
            "letterC" : "Meat"
        }
    ];
   
    var result = "";
    function getData(myLetter) {
        for (var i = 0; i < dataHolder.length; i++) {
       
           if(dataHolder[i].hasOwnProperty(myLetter)){
               console.log(dataHolder[i][myLetter]);
           }
           else{
               console.log("No data found");
           }
            }
        }
    getData("letterA");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.