Categories
JavaScript Answers

How to count unique elements in array without sorting with JavaScript?

Sometimes, we want to count unique elements in array without sorting with JavaScript.

In this article, we’ll look at how to count unique elements in array without sorting with JavaScript.

How to count unique elements in array without sorting with JavaScript?

To count unique elements in array without sorting with JavaScript, we can convert the array to a set and then get its size with the size property.

For instance, we write:

const countUnique = (iterable) => {
  return new Set(iterable).size;
}

console.log(countUnique([5, 6, 5, 6]));

We create the countUniquer function that takes an iterable object.

In the function, we convert iterable to a set with the Set constructor.

And then we get the size of the set with the size property.

Therefore, the console log should log 2.

Conclusion

To count unique elements in array without sorting with JavaScript, we can convert the array to a set and then get its size with the size property.

Categories
JavaScript Answers

How to set a new ID for a element with jQuery?

Sometimes, we want to set a new ID for a element with jQuery.

In this article, we’ll look at how to set a new ID for a element with jQuery.

How to set a new ID for a element with jQuery?

To set a new ID for a element with jQuery, we can call the attr method.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

$('div').attr('id', 'foo')

to select the div with $.

Then we call attr with the attribute name and value respectively.

Therefore, the div should now have the id attribute set to foo.

Conclusion

To set a new ID for a element with jQuery, we can call the attr method.

Categories
JavaScript Answers

How create a table with JavaScript?

Sometimes, we want to create a table with JavaScript.

In this article, we’ll look at how to create a table with JavaScript.

How create a table with JavaScript?

To create a table with JavaScript, we can use the document.createElement and document.createTexNode methods.

For instance, we write:

const table = document.createElement('table');
for (let i = 1; i < 5; i++) {
  const tr = document.createElement('tr');

  const td1 = document.createElement('td');
  const td2 = document.createElement('td');

  const text1 = document.createTextNode('Text1');
  const text2 = document.createTextNode('Text2');

  td1.appendChild(text1);
  td2.appendChild(text2);
  tr.appendChild(td1);
  tr.appendChild(td2);

  table.appendChild(tr);
}
document.body.appendChild(table);

We call document.createElement to create the table element.

Then we add a for loop to create the tr element and the td elements inside the tr.

We create the tr and tds with document.createElement.

Next, we create the text nodes for the td elements with document.createTextNode.

Then we call appendChild to append the text nodes to the tds and append the tds to the trs.

Finally, we call table.appendChild to append the tr to the table element.

And we call document.body.appendChild with table to append the table to the body element.

Conclusion

To create a table with JavaScript, we can use the document.createElement and document.createTexNode methods.

Categories
JavaScript Answers

How to get custom attribute values with jQuery?

Sometimes, we want to get custom attribute values with jQuery.

In this article, we’ll look at how to get custom attribute values with jQuery.

How to get custom attribute values with jQuery?

To get custom attribute values with jQuery, we can use the data method.

For instance, we write:

<div data-something="bar"></div> 

to add a div with the data-something custom attribute.

And then we get the value of data-something by writing:

console.log($("div").data("something"))

We select the div with $.

And then we call data with the part of the attribute name after data- to get its value.

Therefore, the console log logs 'bar' as a result.

Conclusion

To get custom attribute values with jQuery, we can use the data method.

Categories
JavaScript Answers

How to get list of days in a month with JavaScript?

Sometimes, we want to get list of days in a month with JavaScript.

In this article, we’ll look at how to get list of days in a month with JavaScript.

How to get list of days in a month with JavaScript?

To get list of days in a month with JavaScript, we can use the Date constructor and a while loop.

For instance, we write:

const getDaysArray = (year, month) => {
  const monthIndex = month - 1;
  const names = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
  const date = new Date(year, monthIndex, 1);
  const result = [];
  while (date.getMonth() === monthIndex) {
    result.push(date.getDate() + '-' + names[date.getDay()]);
    date.setDate(date.getDate() + 1);
  }
  return result;
}

console.log(getDaysArray(2020, 5))

We create the getDaysArray function that takes the year and month as parameters.

Then we subtract month by 1 and assign that to monthIndex to make sure the months are between 0 and 11 as required by the Date constructor.

Next, we create the names array with the days of the week.

And then we create the first date with the Date constructor.

We then create the result array to store the results.

Next, we create a while loop to check if date.getMonth returns the same value as monthIndex.

If it’s not then date is a date in the next month, so we end the loop.

In the loop body, we call result.push to push the result into the result array.

And the we call date.setDate to change date to the next day.

Finally, we return the results.

Therefore, we get:

 ['1-fri', '2-sat', '3-sun', '4-mon', '5-tue', '6-wed', '7-thu', '8-fri', '9-sat', '10-sun', '11-mon', '12-tue', '13-wed', '14-thu', '15-fri', '16-sat', '17-sun', '18-mon', '19-tue', '20-wed', '21-thu', '22-fri', '23-sat', '24-sun', '25-mon', '26-tue', '27-wed', '28-thu', '29-fri', '30-sat', '31-sun']

from the console log.

Conclusion

To get list of days in a month with JavaScript, we can use the Date constructor and a while loop.