3

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

0

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.