javascript - How to filter elements from an array until specific string -
i'm trying filter out elements array until specific string, in case day names (mon, tue, wed etc.) variable:
var day = new date(reservation.startdate);
i have array of elements , i'm using the push()
method add them inside array.
here array:
console.log(json.stringify(arraydata, null, 4)) [ "<b>mon</b>", "<b>aut14</b>", "<b>kl25ab55200-3001</b>", "<b>mon</b>", "<b>aut15</b>", "<b>kc23dk20010-3003</b>", "<b>tue</b>", "<b>ti14</b>", "<b>kl04bt10110-3001</b>", "<b>tue</b>", "<b>aut15</b>", "<b>kl25ab10451-3001</b>", "<b>tue</b>", "<b>aut13</b>", "<b>aut13</b>", "<b>kl25ad10000-14</b>", "<b>tue</b>", "<b>asr14</b>", "<b>wed</b>", "<b>ti14</b>", "<b>ips16</b>", "<b>a800bd65-3001</b>", "<b>wed</b>", "<b>ti14</b>", "<b>kl04bt10110-3001</b>", "<b>wed</b>", "<b>aut15</b>", "<b>kl25ab55200-3002</b>", "<b>thu</b>", "<b>aut16</b>", "<b>kl25ab10250-3001</b>", "<b>thu</b>", "<b>ti14</b>", "<b>ips16</b>", "<b>a800bd65-3001</b>", "<b>thu</b>", "<b>ti13</b>", "<b>ti14</b>", "<b>kl25ab10300-3001</b>" ]
if want print out data only contains "mon", use:
if (day.tostring().indexof("mon") >= 0) { document.getelementbyid("monday").innerhtml = arraydata.join(""); }
but if use same method on wednesday example, data monday, tuesday , until wednesday, not ideal since want data wednesday.
if (day.tostring().indexof("wed") >= 0) { document.getelementbyid("wednesday").innerhtml = arraydata.join(""); }
so there effective way pick data middle of array , not include other days?
i have use core javascript, frameworks can't used.
to clarify
i need print data each day own innerhtml
sections.
so need data ("monday").innerhtml
:
"<b>mon</b>", "<b>aut14</b>", "<b>kl25ab55200-3001</b>", "<b>mon</b>", "<b>aut15</b>", "<b>kc23dk20010-3003</b>",
("tuesday").innerhtml
:
"<b>tue</b>", "<b>ti14</b>", "<b>kl04bt10110-3001</b>", "<b>tue</b>", "<b>aut15</b>", "<b>kl25ab10451-3001</b>", "<b>tue</b>", "<b>aut13</b>", "<b>aut13</b>", "<b>kl25ad10000-14</b>", "<b>tue</b>", "<b>asr14</b>",
("wednesday").innerhtml
:
"<b>wed</b>", "<b>ti14</b>", "<b>kl04bt10110-3001</b>", "<b>wed</b>", "<b>aut15</b>", "<b>kl25ab55200-3002</b>",
("thursday").innerhtml
:
"<b>thu</b>", "<b>aut16</b>", "<b>kl25ab10250-3001</b>", "<b>thu</b>", "<b>ti14</b>", "<b>ips16</b>", "<b>a800bd65-3001</b>", "<b>thu</b>", "<b>ti13</b>", "<b>ti14</b>", "<b>kl25ab10300-3001</b>"
note data changes day day, should filter out other days , it's data array don't want include in innerhtml
?
simply use array filter function , concatenate conditions if got need , pass "your day".
var array = [ '<b>mon</b>,<b>aut14</b>,<b>kl25ab55200-3001</b>', '<b>mon</b>,<b>aut15</b>,<b>kc23dk20010-3003</b>', '<b>tue</b>,<b>ti14</b>,<b>kl04bt10110-3001</b>', '<b>tue</b>,<b>aut15</b>,<b>kl25ab10451-3001</b>', '<b>tue</b>,<b>aut13</b>,<b>aut14</b>,<b>kl25ad10000-14', '<b>wed</b>,<b>ti14</b>,<b>ips16</b>,<b>a800bd65-3001', '<b>wed</b>,<b>tit14</b>,<b>kl04bt10110-3001</b>', '<b>thu</b><br/>,<b>aut16</b>,<b>kl25ab10250-3001</b>', '<b>thu</b><br/>,<b>ti14</b>,<b>kor16</b>,<b>a800bd65-3001</b>' ]; function filtering(day, el) { return el.includes('<b>'+day+'</b>'); } // printing thu console.log(array.filter(filtering.bind(null, 'thu')).join('')); // printing wed console.log(array.filter(filtering.bind(null, 'wed')).join(''));
edited
i understood data bad arranged , have lot of elements, not grouped row shown in console, here version in order rearrange data , perform operations:
var list = [ '<b>mon</b>','<b>aut14</b>','<b>kl25ab55200-3001</b>', '<b>mon</b>','<b>aut15</b>','<b>kc23dk20010-3003</b>', '<b>tue</b>','<b>ti14</b>','<b>kl04bt10110-3001</b>', '<b>tue</b>','<b>aut15</b>','<b>kl25ab10451-3001</b>', '<b>tue</b>','<b>aut13</b>','<b>aut14</b>','<b>kl25ad10000-14', '<b>wed</b>','<b>ti14</b>','<b>ips16</b>','<b>a800bd65-3001', '<b>wed</b>','<b>tit14</b>','<b>kl04bt10110-3001</b>', '<b>thu</b><br/>','<b>aut16</b>','<b>kl25ab10250-3001</b>', '<b>thu</b><br/>','<b>ti14</b>','<b>kor16</b>','<b>a800bd65-3001</b>' ]; const keywords = ['mon', 'tue', 'wed', 'thu', 'fri']; // starting data // console.log(list); //first reduce array better data format var orderedlist = list.reduce(function(accumulator, currentvalue, currentindex, array) { if (keywords.filter(el => currentvalue.indexof(el) > -1).length > 0) { accumulator.push(currentvalue); } else { accumulator[accumulator.length - 1] += currentvalue; } return accumulator; }, []); // here ordered data // console.log(orderedlist); function filtering(day, el) { return el.includes('<b>'+day+'</b>'); } // printing thu console.log(orderedlist.filter(filtering.bind(null, 'thu')).join('')); // printing wed console.log(orderedlist.filter(filtering.bind(null, 'wed')).join(''));
Comments
Post a Comment