user1996641
user1996641

Reputation: 57

Retrieving values using the key from parsed json using jquery/javascript

using a function(below), i'm trying to get the average house price from my JSON data(below), using the passed in region and house type parameters. I'm ok itterating over and matching on the passed in 'regionName', however I'm then struggling to itterate over the house types (which i believe are the keys in the data) in order to match it against the passed in variable 'literalHouseType', with the aim of then getting that house types price. As you maybe able to tell, the function works up until the point it matches on the region in the first loop, after that I'm stuck. I'm not an avid programmer, so any type of help would be great. Thanks in advance.

var jSonData = JSON.stringify([ {
 "Region_Name": "England",
 "Detached_Average_Price": "357425.4953",
 "Semi_Detached_Average_Price": "219615.157",
 "Terraced_Average_Price": "190095.3402",
 "Flat_Average_Price": "222355.8556" },

 {"Region_Name": "Scotland",
 "Detached_Average_Price": "252491.6457",
 "Semi_Detached_Average_Price": "152351.671",
 "Terraced_Average_Price": "121237.8809",
 "Flat_Average_Price": "104417.6462" }

Function used:

var jSondata = jQuery.parseJSON(jSonData);

$( document ).ready(function() {
GetAveragePriceByHouseTypeAndRegion("Detached_Average_Price", "England");
});

function GetAveragePriceByHouseTypeAndRegion(literalHouseType, regionName) {

for (var i = 0; i < jSondata.length; i++) {
    if (jSondata[i].Region_Name === regionName) {

        for (var j = 0; j < Object.keys(jSondata[i]).length; j++) {                
            if (Object.keys(jSondata[i]).value === literalHouseType) {
             //stuck here..   
            }
        }
    };
}

Upvotes: 0

Views: 27

Answers (1)

user94559
user94559

Reputation: 60143

Assuming literalHouseType will look like "Detached" or "Semi_Detached":

var data = [{
    "Region_Name": "England",
    "Detached_Average_Price": "357425.4953",
    "Semi_Detached_Average_Price": "219615.157",
    "Terraced_Average_Price": "190095.3402",
    "Flat_Average_Price": "222355.8556"
  },

  {
    "Region_Name": "Scotland",
    "Detached_Average_Price": "252491.6457",
    "Semi_Detached_Average_Price": "152351.671",
    "Terraced_Average_Price": "121237.8809",
    "Flat_Average_Price": "104417.6462"
  }
];

function getAveragePriceByHouseTypeAndRegion(literalHouseType, regionName) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].Region_Name === regionName) {
      return data[i][literalHouseType + "_Average_Price"];
    }
  }
}

console.log(getAveragePriceByHouseTypeAndRegion("Semi_Detached", "Scotland"));  // 152351.671

EDIT

Based on the question edit, it looks like a string like Detached_Average_Price is being passed in as literalHouseType, so the code is slightly simpler:

var data = [{
    "Region_Name": "England",
    "Detached_Average_Price": "357425.4953",
    "Semi_Detached_Average_Price": "219615.157",
    "Terraced_Average_Price": "190095.3402",
    "Flat_Average_Price": "222355.8556"
  },

  {
    "Region_Name": "Scotland",
    "Detached_Average_Price": "252491.6457",
    "Semi_Detached_Average_Price": "152351.671",
    "Terraced_Average_Price": "121237.8809",
    "Flat_Average_Price": "104417.6462"
  }
];

function getAveragePriceByHouseTypeAndRegion(literalHouseType, regionName) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].Region_Name === regionName) {
      return data[i][literalHouseType];
    }
  }
}

console.log(getAveragePriceByHouseTypeAndRegion("Semi_Detached_Average_Price", "Scotland"));  // 152351.671

EDIT 2

Of course, if you can reshape your data, this is even easier:

var data = {
  "England": {
    "Detached_Average_Price": "357425.4953",
    "Semi_Detached_Average_Price": "219615.157",
    "Terraced_Average_Price": "190095.3402",
    "Flat_Average_Price": "222355.8556"
  },
  "Scotland": {
    "Detached_Average_Price": "252491.6457",
    "Semi_Detached_Average_Price": "152351.671",
    "Terraced_Average_Price": "121237.8809",
    "Flat_Average_Price": "104417.6462"
  }
};

function getAveragePriceByHouseTypeAndRegion(literalHouseType, regionName) {
  return data[regionName][literalHouseType];
}

console.log(getAveragePriceByHouseTypeAndRegion("Semi_Detached_Average_Price", "Scotland")); // 152351.671

Upvotes: 1

Related Questions