I want to customise an array to some types using jquery
$(function(){
let i = 1;
$('.dataTable thead th').each(function(index) {
let array = {
text:$(this).text(),
if($(this).hasClass("datepicker")) {
type:"number"
}
};
});
});
I want to say that if this has class datepicker so set the type:number
and else set the type:text
What you have is not an array. Perhaps you meant
$(function() {
var arr = [];
$('.dataTable thead th').each(function(index) {
arr.push({
"type": $(this).hasClass("datepicker")?"number":"text";
});
});
});
or
$(function() {
var arr = [];
$('.dataTable thead th').each(function(index) {
arr.push({
"text": $.trim($(this).text()) || "n/a",
"type": $(this).hasClass("datepicker")?"number":"text";
});
});
});
You can streamline this using http://api.jquery.com/map/
try
let table= [...document.querySelectorAll('.dataTable thead th')];
table.forEach(e=> {
let arr = {
type: e.classList.contains('datepicker') ? 'text' : 'number',
text: e.innerText,
}
console.log(arr);
});
<table class="dataTable"><thead>
<th>A</th>
<th class='datepicker'>B</th>
<th>C</th>
</thead></table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With