javascript - How to find "title" of multi-dimensional array where value was found? -
i'm trying "title" of nested array matched date found...but no avail.
if date found in array, should alert "true". date has match or within date range. when found, i'd "title" of nested array found match. (ie. date "10/29/2013" found match under "person 2". i'd title well.
i'm looping through both nested arrays check match on date. eventually, they'll lot more people , times. note, no time show twice within array. used display available during time on webpage. while database better route go, sitting on mainframe , not tied db2.
if there easier way of going that, i'm ears well.
var data = [] var data = [ {title:'person 1',contents:[ {primary:"7/09/2013"},{primary:"7/22/2013"},{primary:"10/15/2013"},{primary:"10/28/2013"},{primary:"1/21/2014"},{primary:"2/03/2014"}]}, {title:'person 2',contents:[ {primary:"7/23/2013"},{primary:"8/05/2013"},{primary:"10/29/2013"},{primary:"11/11/2013"},{primary:"2/04/2014"},{primary:"2/17/2014"}] } ]; array.prototype.between = function(arg) { for(var i=0, j=data.length; < j; i++){ for(var p=0, plen=data[i].contents.length; p < plen; p++){ var d1 = new date(data[i].contents[0].primary), d2 = new date(data[i].contents[1].primary), d4 = new date(data[i].contents[2].primary), d5 = new date(data[i].contents[3].primary), d3 = new date(arg); } //alert(data[i].title); //using "data[i].title" grab both titles need one. } return ((d1.gettime() <= d3.gettime() && d3.gettime() <= d2.gettime()) || (d4.gettime() <= d3.gettime() && d3.gettime() <= d5.gettime())); } alert(data.between("10/29/2013"));
and fiddle...
i'm pretty sure code should this:
array.prototype.between = function(arg) { for(var i=0, j=data.length; < j; i++){ for(var p=0, plen=data[i].contents.length; p < plen; p++){ var d1 = new date(data[i].contents[0].primary), d2 = new date(data[i].contents[1].primary), d4 = new date(data[i].contents[2].primary), d5 = new date(data[i].contents[3].primary), d3 = new date(arg); if ((d1.gettime() <= d3.gettime() && d3.gettime() <= d2.gettime()) || (d4.gettime() <= d3.gettime() && d3.gettime() <= d5.gettime())) return data[i].title; } } }
when call function, it'll return either title or else undefined
if date doesn't match.
Comments
Post a Comment