Keerthivasan Keerthivasan - 1 month ago 15
Javascript Question

checking JSON Data contains media or not..?

I am rendering the summary with JSON Data, in that i am checking for media.,
if media(image,audio,video) exist in JSON Data, it should render corresponding media to screen. so i am checking every value that contains media or not based on filetype. i've facing the problem while checking different data values

Here My JS Code

var disp=[
{
"Session_ID": 1,
"column1": "http://dev.starport.in/phpupload/dentist.jpeg",
"Session_Name": "0",
"Session_Code": "0",
"Start_Time": "09:30",
"End_Time": "09:30",
"Staff": 0,
"Date": "/Date(-2188013400000)/",
"Status": 1,
"RowNumber": 1
}
];
var mediatype;
var media;
var fname;
$.each(disp[0],function(key,value)
{
fname=value;
media=0;
var filetype;

if(fname.indexOf(".")> -1) // problem
filetype = fname.split('.').pop();
else
return true;

if($.inArray(filetype, ['gif','png','jpg','jpeg']) >= 1) {
media=1;
mediatype="image";
return false;
}
else if($.inArray(filetype, ['mkv','mp4','vob']) >= 1) {
media=1;
mediatype="video";
return false;
}
else if(filetype == "mp3") {
media=1;
mediatype="audio";
return false;
}
else
{
media=0;
}
});
console.log(mediatype);


Hence my code works for string and also render appropriate item, when its comes to other format occurs errors..

Awaiting Responses Thanks in Advance

Answer

This code will iterate over all the item inside the disp array and for each item it will iterate his key - value.

Your problem was that the first value is an int thus you cannot use indexOf. You should first check that the value is a string

var disp = [{
  "Session_ID": 1,
  "column1": "http://dev.starport.in/phpupload/dentist.jpeg",
  "Session_Name": "0",
  "Session_Code": "0",
  "Start_Time": "09:30",
  "End_Time": "09:30",
  "Staff": 0,
  "Date": "/Date(-2188013400000)/",
  "Status": 1,
  "RowNumber": 1
}];
var mediatype;
var media;
var fname;

disp.forEach(function(item) {
  var media = 0;
  var filetype;

  for (var key in item) {
    if (typeof item[key] === "string") {
      if (item[key].indexOf(".") > -1) {
        filetype = item.column1.split(".").pop();
        break;
      }
    }
  }
  var media = 0;
  var filetype;

  if (item.column1.indexOf(".") > -1) {
    filetype = item.column1.split(".").pop();
  }

  if ($.inArray(filetype, ['gif', 'png', 'jpg', 'jpeg']) >= 1) {
    media = 1;
    mediatype = "image";
    return false;
  } else if ($.inArray(filetype, ['mkv', 'mp4', 'vob']) >= 1) {
    media = 1;
    mediatype = "video";
    return false;
  } else if (filetype == "mp3") {
    media = 1;
    mediatype = "audio";
    return false;
  } else {
    media = 0;
  }
});

console.log(mediatype);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

NOTE

This is a very bad practice. You should know where is your media stored. It would be very weird to store your media inside End_Time for example.

The best solution would be to change your API and the JSON sent

Comments