I have a data table that has a structure like:
Solution 1:
Here are 3 versions
ES6 with fat arrows ES2015 with function Legacy JS which should run from IE8 or so
const uniqueCount = [...document .querySelectorAll ("td[id^=invNumbers]" )]
.reduce ((acc, cur ) => {
const val = cur.textContent ;
if (!acc.includes (val)) acc.push (val);
return acc;
}, []).length ;
console .log (uniqueCount)
const uniqueCount1 = [...document .querySelectorAll ("td[id^=invNumbers]" )]
.reduce (function (acc, cur ) {
const val = cur.textContent ;
if (!acc.includes (val)) acc.push (val);
return acc;
}, []).length ;
console .log (uniqueCount1)
var cells = document .querySelectorAll ("td[id^=invNumbers]" ); var arr = [];
for (var i = 0 ; i < cells.length ; i++) {
var val = cells[i].innerText ;
if (arr.indexOf (val) ===-1 ) arr.push (val);
}
console .log (arr.length )
Copy <table > <tbody > <tr > <td > ABC</td > <td > </td > <td id ="invNumbers0" > 12345</td > <td > </td > </tr > </tbody > <tbody > <tr > <td > GHI</td > <td > </td > <td id ="invNumbers1" > 12345</td > <td > </td > </tr > <tr > <td > MNO</td > <td > </td > <td id ="invNumbers2" > 4566</td > <td > </td > </tr > <tr > <td > STU</td > <td > </td > <td id ="invNumbers3" > 12345</td > <td > </td > </tr > <tr > <td > ABC</td > <td > </td > <td id ="invNumbers4" > 2566</td > <td > </td > </tr > </tbody > <tbody > <tr > <td > GHI</td > <td > </td > <td id ="invNumbers5" > 4566</td > <td > </td > </tr > <tr > <td > MNO</td > <td > </td > <td id ="invNumbers6" > 12345</td > <td > </td > </tr > <tr > <td > STU</td > <td > </td > <td id ="invNumbers7" > 12345</td > <td > </td > </tr > </tbody > </table >
Copy Solution 2:
You could try something like this:
var arr = $('td[id^=invNumbers]' ).map (function ( ) {
return $(this ).text ()
}).get ();
var unique = arr.filter (function (v, i, a ) {
return a.indexOf (v) === i
});
Copy Demo
var arr = $('td[id^=invNumbers]' ).map (function ( ) {
return $(this ).text ()
}).get ();
var unique = arr.filter (function (v, i, a ) {
return a.indexOf (v) === i
});
console .log (unique);
Copy <script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" > </script > <table > <tbody > <tr > <td > ABC</td > <td > </td > <td id ="invNumbers0" > 12345</td > <td > </td > </tr > </tbody > <tbody > <tr > <td > GHI</td > <td > </td > <td id ="invNumbers1" > 12345</td > <td > </td > </tr > <tr > <td > MNO</td > <td > </td > <td id ="invNumbers2" > 4566</td > <td > </td > </tr > <tr > <td > STU</td > <td > </td > <td id ="invNumbers3" > 12345</td > <td > </td > </tr > <tr > <td > ABC</td > <td > </td > <td id ="invNumbers4" > 2566</td > <td > </td > </tr > </tbody > <tbody > <tr > <td > GHI</td > <td > </td > <td id ="invNumbers5" > 4566</td > <td > </td > </tr > <tr > <td > MNO</td > <td > </td > <td id ="invNumbers6" > 12345</td > <td > </td > </tr > <tr > <td > STU</td > <td > </td > <td id ="invNumbers7" > 12345</td > <td > </td > </tr > </tbody > </table >
Copy
Post a Comment for "Getting The Count Of Unique Values For Elements With Regex In Datatable"