Categories
JavaScript Answers

How to iterate over an object with JavaScript and Handlebars.js?

Sometimes, we want to iterate over an object with JavaScript and Handlebars.js.

In this article, we’ll look at how to iterate over an object with JavaScript and Handlebars.js.

How to iterate over an object with JavaScript and Handlebars.js?

To iterate over an object with JavaScript and Handlebars.js, we can use the #each keyword.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>

<script id="some-template" type="text/x-handlebars-template">
  <option value="none">select</option>
{{#each data}}
    <optgroup label="{{@key}}">
    {{#each this}}
        <option value="{{id}}">{{name}}</option>
    {{/each}}
    </optgroup>
{{/each}}
</script>

<select>

</select>

to add the Handlebars script and the template for looping through each entry in data.

We use #each this to loop through each array property.

@key returns the property name.

Then we write:

const source = $("#some-template").html();
const template = Handlebars.compile(source);
const data = {
  data: {
    fruit: [{
      id: 1,
      name: "banana"
    }, {
      id: 2,
      name: "apple"
    }],
    vegetables: [{
      id: 1,
      name: "broccoli"
    }]
  }
}

$('select').append(template(data));

to select the template script tag and return its HTML code with:

const source = $("#some-template").html();

Then we compile the template with:

const template = Handlebars.compile(source);

And finally, we render the template with the data with:

$('select').append(template(data));

Now we should see a drop down with the option groups rendered from the data object.

Conclusion

To iterate over an object with JavaScript and Handlebars.js, we can use the #each keyword.

Categories
JavaScript Answers

How to measure text width and height without rendering with JavaScript?

Sometimes, we want to measure text width and height without rendering with JavaScript.

In this article, we’ll look at how to measure text width and height without rendering with JavaScript.

How to measure text width and height without rendering with JavaScript?

To measure text width and height without rendering with JavaScript, we can put the text in a canvas element and measure it there.

For instance, we write:

const getTextSize = (txt, font) => {
  const element = document.createElement('canvas');
  const context = element.getContext("2d");
  context.font = font;
  const tSize = {
    'width': context.measureText(txt).width,
    'height': parseInt(context.font)
  };
  return tSize;
}

const tSize = getTextSize("Hello World", "30px Arial");
console.log(tSize)

to define the getTextSize function that takes the txt text and font.

In it, we create the canvas element with document.createElement.

Then we call element.getContext to get the context.

Next, we set the font of the context.

And then we do the measure with the context.measureText method.

And we get the font height with context.font.

As a result, tSize should be {width: 154.4970703125, height: 30} according to the console log.

Conclusion

To measure text width and height without rendering with JavaScript, we can put the text in a canvas element and measure it there.

Categories
JavaScript Answers

How to read standard input with JavaScript?

Sometimes, we want to read standard input with JavaScript.

In this article, we’ll look at how to read standard input with JavaScript.

How to read standard input with JavaScript?

To read standard input with JavaScript, we can use the readline module.

For instance, we write:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout

});

rl.question("> >What's your name?  ", (answer) => {
  console.log("Hello", answer);
  rl.close();
});

We call readline.createInteface with:

{
  input: process.stdin,
  output: process.stdout

}

to read standard input and output standard output.

Then we call rl.question to show a prompt for the user.

And the entered answer will be set as the value of the answer parameter.

Finally, we call rl.close to stop reading standard input.

Conclusion

To read standard input with JavaScript, we can use the readline module.

Categories
JavaScript Answers

How to add named function parameters with JavaScript?

Sometimes, we want to add named function parameters with JavaScript.

In this article, we’ll look at how to add named function parameters with JavaScript.

How to add named function parameters with JavaScript?

To add named function parameters with JavaScript, we can make a function accept an object as a parameter.

Then we can destructure the object’s properties into variables.

For instance, we write:

const callApi = ({
  url,
  callback,
  query = {},
  body = {}
}) => {

}

callApi({
  url: "/api/..",
  callback: (x => console.log(x)),
  body: {
    a: 2
  }
})

We have the callApi function that takes an object with the url, callback, query and body properties.

We set the default value of query and body to an empty when it’s not set.

Then we call it with an object with those properties set.

Conclusion

To add named function parameters with JavaScript, we can make a function accept an object as a parameter.

Then we can destructure the object’s properties into variables.

Categories
JavaScript Answers

What is the equivalent of Ruby’s binding.pry for JavaScript console?

Sometimes, we want to use is the equivalent of Ruby’s binding.pry for JavaScript console to debug JavaScript apps.

In this article, we’ll look at what is the equivalent of Ruby’s binding.pry for JavaScript console to debug JavaScript apps.

What is the equivalent of Ruby’s binding.pry for JavaScript console?

The equivalent of Ruby’s binding.pry for JavaScript console to debug JavaScript apps is the debugger keyword.

To use it, we add debugger into our code.

And when the browser developer tools is open, the app will stop at the point where the debugger keyword is at.

For Node.js apps, we can use node debug appname.js with `debugger to achieve equivalent functionality

Conclusion

The equivalent of Ruby’s binding.pry for JavaScript console to debug JavaScript apps is the debugger keyword.