'how to set a condition in an array using jquery?

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



Solution 1:[1]

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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamil Kiełczewski