bindiego
4/3/2015 - 2:25 AM

JavaScript Utility

JavaScript Utility

/**
 * String utility
 */
function isNotEmpty(string) {
    if(!string)             return false;         
    if(string == '')        return false;
    if(string === false)    return false; 
    if(string === null)     return false; 
    if(string == undefined) return false;
    string = string+' '; // check for a bunch of whitespace
    if('' == (string.replace(/^\s\s*/, '').replace(/\s\s*$/, ''))) return false;       
    return true;        
}
// create question objects
var q1 = {question:"your question 1"};
var q2 = {question:"your question 2"};
// creat question array
var questionList = new Array();
// add question to List
questionList.push(q1);
questionList.push(q2);
var yourJsonString = JSON.stringify(questionList);
console.log("jsonstring of questions: " + yourJsonString);

var newList = JSON.parse(yourJsonString);
// loop through the parsed Array Object
for (var i in newList) {
   // use tmp to get the question object
   var tmp = newList[i];
   // do what ever you want
   console.log(tmp.question);
}

/* output
jsonstring of questions: [{"question":"your question 1"},{"question":"your question 2"}]
your question 1
your question 2
*/
/*
 * Array utility
 */
function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}