Categories
JavaScript Answers

How to find by key deep in a nested array with JavaScript?

Sometimes, we want to find by key deep in a nested array with JavaScript.

In this article, we’ll look at how to find by key deep in a nested array with JavaScript.

How to find by key deep in a nested array with JavaScript?

To find by key deep in a nested array with JavaScript, we can recursively search each level of the nested array.

For instance, we write

const deepSearchByKey = (object, originalKey, matches = []) => {
  if (object !== null) {
    if (Array.isArray(object)) {
      for (const arrayItem of object) {
        deepSearchByKey(arrayItem, originalKey, matches);
      }
    } else if (typeof object === "object") {
      for (const key of Object.keys(object)) {
        if (key === originalKey) {
          matches.push(object);
        } else {
          deepSearchByKey(object[key], originalKey, matches);
        }
      }
    }
  }

  return matches;
};

to define the deepSearchByKey function.

In it, we check if object isn’t null.

If it is, then we check if object is an array with Array.isArray.

If it is, then we call deepSearchByKey to search for the key we’re looking for.

Otherwise, if object is an object, which we check with typeof object === "object", then we loop through the keys of the object that we get from Object.keys.

If key equals originalKey then we push the object to matches.

Otherwise, we call deepSearchByKey to search the object[key] object.

Conclusion

To find by key deep in a nested array with JavaScript, we can recursively search each level of the nested array.

Categories
JavaScript Answers

How to declare array of objects with JavaScript?

Sometimes, we want to declare array of objects with JavaScript.

In this article, we’ll look at how to declare array of objects with JavaScript.

How to declare array of objects with JavaScript?

To declare array of objects with JavaScript, we can use the array fill method.

For instance, we write

const arr = new Array(5).fill({ test: "a" });

to create an array with 5 empty slots with Array(5).

Then we call fill with each slot with the { test: "a" } object.

Conclusion

To declare array of objects with JavaScript, we can use the array fill method.

Categories
JavaScript Answers

How to change the value of the Bootstrap 4 file input with JavaScript?

Sometimes, we want to change the value of the Bootstrap 4 file input with JavaScript.

In this article, we’ll look at how to change the value of the Bootstrap 4 file input with JavaScript.

How to change the value of the Bootstrap 4 file input with JavaScript?

To change the value of the Bootstrap 4 file input with JavaScript, we can get the element with the value text and change it to the new value when a file is selected.

For instance, we write

document.querySelector(".custom-file-input").addEventListener("change", (e) => {
  const fileName = document.getElementById("myInput").files[0].name;
  const nextSibling = e.target.nextElementSibling;
  nextSibling.innerText = fileName;
});

to listen to the change event of the custom file input with addEventListener.

In the change event listener, we get the selected file’s name with

document.getElementById("myInput").files[0].name

Then we get the element with the custom file input’s value with

e.target.nextElementSibling

And we change the element’s innerText to the fileName to show the selected file’s name.

Conclusion

To change the value of the Bootstrap 4 file input with JavaScript, we can get the element with the value text and change it to the new value when a file is selected.

Categories
JavaScript Answers

How to get lat/lng from Google marker with JavaScript?

Sometimes, we want to get lat/lng from Google marker with JavaScript.

In this article, we’ll look at how to get lat/lng from Google marker with JavaScript.

How to get lat/lng from Google marker with JavaScript?

To get lat/lng from Google marker with JavaScript, we can listen to the marker’s dragstart and dragend events.

For instance, we write

<div id="map_canvas"></div>

to add a div for the map.

Then we set its size with

#map_canvas {
  width: 400px;
  height: 300px;
}

Then we create the map with a marker inside with

const map = new google.maps.Map(document.getElementById("map_canvas"), {
  zoom: 1,
  center: new google.maps.LatLng(45.137879, -82.836914),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
});

const myMarker = new google.maps.Marker({
  position: new google.maps.LatLng(47.651968, 9.478485),
  draggable: true,
});

google.maps.event.addListener(myMarker, "dragend", (evt) => {
  console.log(evt.latLng.lng().toFixed(3));
});

google.maps.event.addListener(myMarker, "dragstart", (evt) => {
  console.log("dragging marker");
});

We use the Map constructor to fill the div with the map contents.

And we set the default zoom and center of the map.

Next, we create a marker with the Marker constructor.

And then we call addListener to listen to the dragstart and dragend events of the marker.

In the dragend callback, we get the longitude of the marker’s current location with evt.latLng.lng().

Conclusion

To get lat/lng from Google marker with JavaScript, we can listen to the marker’s dragstart and dragend events.

Categories
JavaScript Answers

How to get the full height of a HTML element including border, padding and margin with JavaScript?

Sometimes, we want to get the full height of a HTML element including border, padding and margin with JavaScript.

In this article, we’ll look at how to get the full height of a HTML element including border, padding and margin with JavaScript.

How to get the full height of a HTML element including border, padding and margin with JavaScript?

To get the full height of a HTML element including border, padding and margin with JavaScript, we can use the getComputedStyle method.

For instance, we write

const el = document.querySelector("div");

let elHeight = el.offsetHeight;
elHeight += parseInt(
  window.getComputedStyle(el).getPropertyValue("margin-top")
);
elHeight += parseInt(
  window.getComputedStyle(el).getPropertyValue("margin-bottom")
);

console.log(elHeight);

to get the div with querySelector.

Then we get the height of it without the margins with offsetHeight.

Next, we call getComputedStyle with el to get the margin top and margin bottom values of the div.

And then we add them to elHeight.

Conclusion

To get the full height of a HTML element including border, padding and margin with JavaScript, we can use the getComputedStyle method.