json_column

json_column(array,key) array: Array object containing JSON objects key: String object for the key to return from each JSON object return: an array containing all JSON values using the searched key. note: this function will throw an error if at least one JSON object doesnt have the searched key.

by Amine Tbaik

JavaScript

function json_column(array, param) {
  result = []
  if (!Array.isArray(array)) {
    throw Error("json_column is expecting parameter 1 to be an Array, " + typeof array + " is passed instead")
  } else if (typeof param != "string") {
    if (typeof param === "undefined") {
      throw Error("missing parameter 2 for json_column")
    } else {
      throw Error("json_column is expecting parameter 2 to be a String, " + typeof param + " is passed instead")
    }
  } else {
    $.each(array, function(i, json) {
    	if(json[param] == null){
      	throw Error("not all json elements has "+param+" key")
      }else{
        result.push(json[param])
        console.log(json[param])      
      }
    })
    return result
  }
}