Categories
JavaScript Answers

How to access nested JSON data with JavaScript?

Sometimes, we want to access nested JSON data with JavaScript.

In this article, we’ll look at how to access nested JSON data with JavaScript.

How to access nested JSON data with JavaScript?

To access nested JSON data with JavaScript, we can use the Lodash get method.

For instance, we write:

const data = {
  "id": 1,
  "name": "abc",
  "address": {
    "streetName": "cde",
    "streetId": 2
  }
}
const streetName = _.get(data, 'address.streetName');
console.log(streetName)

to define the data object.

Then we get the value of the data.address.streetName property with _.get(data, 'address.streetName');.

As a result, streetName is 'cde'.

Conclusion

To access nested JSON data with JavaScript, we can use the Lodash get method.

Categories
JavaScript Answers

How to detect if an iframe is cross domain with JavaScript?

Sometimes, we want to detect if an iframe is cross domain with JavaScript.

In this article, we’ll look at how to detect if an iframe is cross domain with JavaScript.

How to detect if an iframe is cross domain with JavaScript?

To detect if an iframe is cross domain with JavaScript, we can check if iframe.contentDocument is defined.

For instance, we write:

<iframe src='https://example.com'>

</iframe>

to add an iframe.

Then we write:

const iframe = document.querySelector('iframe')
const canAccessIframe = (iframe) => {
  try {
    return Boolean(iframe.contentDocument);
  } catch (e) {
    return false;
  }
}
console.log(canAccessIframe(iframe))

to select the iframe with querySelector.

Then we define the canAccessIFrame function that checks if the iframe has the contentDocument property defined.

If it’s defined then it’s not a cross-domain iframe or it’s cross domain and cross domain is allowed.

Otherwise, false is returned.

Conclusion

To detect if an iframe is cross domain with JavaScript, we can check if iframe.contentDocument is defined.

Categories
JavaScript Answers

How to resize HTML5 canvas to parent’s size with JavaScript?

Sometimes, we want to resize HTML5 canvas to parent’s size with JavaScript.

In this article, we’ll look at how to resize HTML5 canvas to parent’s size with JavaScript.

How to resize HTML5 canvas to parent’s size with JavaScript?

To resize HTML5 canvas to parent’s size with JavaScript, we can set the canvas’ width and height properties.

For instance, we write:

<div>
  <canvas></canvas>
</div>

to add a div and a canvas.

Then we write:

const canvas = document.querySelector('canvas')
const theDiv = document.querySelector('div')
canvas.width = theDiv.clientWidth;
canvas.height = theDiv.clientHeight;

to resize the canvas to the size of the div by setting the canvas‘s height and width to the theDivs‘s clientHeight and clientWidth properties respectively.

As a result, the size of the canvas is now the same as as the parent div.

Conclusion

To resize HTML5 canvas to parent’s size with JavaScript, we can set the canvas’ width and height properties. to select the canvas and a div.

Categories
JavaScript Answers

How to get the text after a specific word with JavaScript?

Sometimes, we want to get the text after a specific word with JavaScript.

In this article, we’ll look at how to get the text after a specific word with JavaScript.

How to get the text after a specific word with JavaScript?

To get the text after a specific word with JavaScript, we can use some string methods.

For instance, we write:

const x = 'foobarbaz'
const y = 'bar'
const z = x.slice(x.indexOf(y) + y.length);
console.log(z)

to get the part of x after 'bar'.

To do this, we call x.slice with the index of the first character of y, which we get from indexOf.

We didn’t pass in a second argument to slice, so the end index is the end of the string.

Then we add y.length to it to get the last index of 'bar' in x.

Therefore, z is 'baz'.

Conclusion

To get the text after a specific word with JavaScript, we can use some string methods.

Categories
JavaScript Answers

How to make a chainable function with JavaScript?

Sometimes, we want to make a chainable function with JavaScript.

In this article, we’ll look at how to make a chainable function with JavaScript.

How to make a chainable function with JavaScript?

To make a chainable function with JavaScript, we can return this in our methods.

For instance, we write:

const obj = {
  foo() {
    this.foo = 'foo';
    return this;
  },
  bar() {
    this.bar = 'bar';
    return this;
  }
}
obj.foo().bar()
console.log(obj)

to define the obj object that had the foo and bar methods that sets the value of this.foo and this.bar respectively and returns this.

Since this is obj, we can call foo and bar by chaining them.

As a result, obj is {foo: 'foo', bar: 'bar'} after we run obj.foo().bar().

Conclusion

To make a chainable function with JavaScript, we can return this in our methods.