Categories
JavaScript Answers

How to push to an array only if value doesn’t exist with JavaScript?

Sometimes, we want to push to an array only if value doesn’t exist with JavaScript.

In this article, we’ll look at how to push to an array only if value doesn’t exist with JavaScript.

How to push to an array only if value doesn’t exist with JavaScript?

To push to an array only if value doesn’t exist with JavaScript, we use a set.

For instance, we write

const s = new Set();

s.add("hello");
s.add("world");
s.add("hello");
s.delete("world");

const array = Array.from(s);

to use the Set constructor to create a set.

Then we call add to add entries to it.

And we call delete to delete entries from it.

Next, we call Array.from to convert set s to an array.

Conclusion

To push to an array only if value doesn’t exist with JavaScript, we use a set.

Categories
JavaScript Answers

How to remove all series data from a HighCharts chart with JavaScript?

Sometimes, we want to remove all series data from a HighCharts chart with JavaScript.

In this article, we’ll look at how to remove all series data from a HighCharts chart with JavaScript.

How to remove all series data from a HighCharts chart with JavaScript?

To remove all series data from a HighCharts chart with JavaScript, we call the remove method.

For instance, we write

while (chart.series.length > 0) {
  chart.series[0].remove(true);
}

to loop through the chart series with a while loop.

Then we get the first one with chart.series[0].

And we call remove with true to remove it.

Conclusion

To remove all series data from a HighCharts chart with JavaScript, we call the remove method.

Categories
JavaScript Answers

How to hide the Android keyboard using JavaScript?

Sometimes, we want to hide the Android keyboard using JavaScript.

In this article, we’ll look at how to hide the Android keyboard using JavaScript.

How to hide the Android keyboard using JavaScript?

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

For instance, we write

const field = document.createElement("input");
field.setAttribute("type", "text");
document.body.appendChild(field);

setTimeout(() => {
  field.focus();
  setTimeout(() => {
    field.setAttribute("style", "display: none;");
  }, 50);
}, 50);

to call field.focus to focus on the input.

Then we call field.setAttribute with 'style' and 'display: none' to hide the field.

We do both with a delay by putting them in a setTimeout callback.

Conclusion

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

Categories
React Native Answers

How to fit Image in containing View, not the whole screen size with React Native?

Sometimes, we want to fit Image in containing View, not the whole screen size with React Native.

In this article, we’ll look at how to fit Image in containing View, not the whole screen size with React Native.

How to fit Image in containing View, not the whole screen size with React Native?

To fit Image in containing View, not the whole screen size with React Native, we set resizeMode to cover.

For instance, we write

const style = StyleSheet.create({
  imageStyle: {
    alignSelf: "center",
    height: "100%",
    width: "100%",
  },
});

//...

const Comp = () => {
  //...
  return (
    <View>
      <Image
        style={styles.imageStyle}
        resizeMode={"cover"}
        source={item.image}
      />
    </View>
  );
};

to add an Image with the resizeMode prop set to cover.

This will make it fit to the View wrapped around it.

Categories
JavaScript Answers

How to set an option’s selected attribute from dynamic created option with JavaScript?

Sometimes, we want to set an option’s selected attribute from dynamic created option with JavaScript/

In this article, we’ll look at how to set an option’s selected attribute from dynamic created option with JavaScript.

How to set an option’s selected attribute from dynamic created option with JavaScript?

To set an option’s selected attribute from dynamic created option with JavaScript, we set the selected property of the option.

For instance, we write

const country = document.getElementById("country");
country.options[country.options.selectedIndex].selected = true;

to select the select element with getElementById.

And we get the selected option with country.options[country.options.selectedIndex]

We set its selected property to true to make it selected.

Conclusion

To set an option’s selected attribute from dynamic created option with JavaScript, we set the selected property of the option.