Categories
JavaScript Answers

How to remove all classes that begin with a certain string with JavaScript?

Sometimes, we want to remove all classes that begin with a certain string with JavaScript.

In this article, we’ll look at how to remove all classes that begin with a certain string with JavaScript.

How to remove all classes that begin with a certain string with JavaScript?

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

For instance, we write

const prefix = "prefix";
const classes = el.className.split(" ").filter((c) => {
  return !c.startsWith(prefix);
});
el.className = classes.join(" ").trim();

to call el.className.split to split the className string by the spaces into an array of substrings.

Then we call filter with a callback that filters out all entries that starts with prefix.

Next, we call join to join all the class name strings in the classes array back into a string separated by spaces.

Then we call trim to remove start and ending whitespaces.

Conclusions

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

Categories
JavaScript Answers

How to check if the array of objects have duplicate property values with JavaScript?

Sometimes, we want to check if the array of objects have duplicate property values with JavaScript.

In this article, we’ll look at how to check if the array of objects have duplicate property values with JavaScript.

How to check if the array of objects have duplicate property values with JavaScript?

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.

For instance, we write

const values = [
  { name: "someName1" },
  { name: "someName2" },
  { name: "someName3" },
  { name: "someName1" },
];

const uniqueValues = new Set(values.map((v) => v.name));

if (uniqueValues.size < values.length) {
  console.log("duplicates found");
}

to get the name property values of each values array entry with

values.map((v) => v.name)

Then we convert the array to a set with the Set constructor.

If the set’s size is less than valueslength, then we know there’re duplicates values in values.

Conclusion

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.

Categories
JavaScript Answers

How to merge two array of objects based on a key with JavaScript?

Sometimes, we want to merge two array of objects based on a key with JavaScript.

In this article, we’ll look at how to merge two array of objects based on a key with JavaScript.

How to merge two array of objects based on a key with JavaScript?

To merge two array of objects based on a key with JavaScript, we can use the array map and find methods and the spread operator.

For instance, we write

const arr1 = [
  { id: "abdc4051", date: "2017-01-24" },
  { id: "abdc4052", date: "2017-01-22" },
];

const arr2 = [
  { id: "abdc4051", name: "ab" },
  { id: "abdc4052", name: "abc" },
];

const mergeById = (a1, a2) =>
  a1.map((itm) => ({
    ...a2.find((item) => item.id === itm.id && item),
    ...itm,
  }));

console.log(mergeById(arr1, arr2));

to define the mergeById function.

In it, we call a1.map with a callback that has the contents of the object in a2 that has the same id of the current object in a1, which is itm and the itm object.

We find the object with the same id value as the one being looped through in a1 with

a2.find((item) => item.id === itm.id && item)

We spread the properties of each into a new object and return it.

Conclusion

To merge two array of objects based on a key with JavaScript, we can use the array map and find methods and the spread operator.

Categories
JavaScript Answers

How to limit labels number on Chart.js line chart with JavaScript?

Sometimes, we want to limit labels number on Chart.js line chart with JavaScript.

In this article, we’ll look at how to limit labels number on Chart.js line chart with JavaScript.

How to limit labels number on Chart.js line chart with JavaScript?

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit property.

For instance, we write

const options = {
  scales: {
    x: {
      ticks: {
        maxTicksLimit: 10,
      },
    },
  },
};

const myLineChart = new Chart(ctx, {
  type: "line",
  data,
  options,
});

to create a Chart object with the canvas context ctx and an object with the options property.

We set the options.scales.x.ticks.maxTicksLimit property to 10 to limit the number of x-axis ticks to 10.

Conclusion

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit property.

Categories
JavaScript Answers

How to convert JSON object to JavaScript array?

Sometimes, we want to convert JSON object to JavaScript array.

In this article, we’ll look at how to convert JSON object to JavaScript array.

How to convert JSON object to JavaScript array?

To convert JSON object to JavaScript array, we can use the Object.entries method.

For instance, we write

const jsonData = { "2022-01-21": 1, "2022-01-22": 7 };

console.log(Object.entries(jsonData));

to call Object.entries with the jsonData object to return an array with the key-value pairs of the jsonData object as arrays.

Conclusion

To convert JSON object to JavaScript array, we can use the Object.entries method.