Categories
JavaScript Answers

How to get first three characters of a string with JavaScript?

Sometimes, we want to get first three characters of a string with JavaScript.

In this article, we’ll look at how to get first three characters of a string with JavaScript.

How to get first three characters of a string with JavaScript?

To get first three characters of a string with JavaScript, we can use the string substring method.

For instance, we write

const str = "012123";
const strFirstThree = str.substring(0, 3);

console.log(strFirstThree);

to call str.substring with 0 and 3 to return a string with the first 3 characters of str.

Conclusion

To get first three characters of a string with JavaScript, we can use the string substring method.

Categories
JavaScript Answers

How to modify object property in an array of objects with JavaScript?

Sometimes, we want to modify object property in an array of objects with JavaScript.

In this article, we’ll look at how to modify object property in an array of objects with JavaScript.

How to modify object property in an array of objects with JavaScript?

To modify object property in an array of objects with JavaScript, we can use the array find method.

For instance, we write

const foo = [
  { bar: 1, baz: [1, 2, 3] },
  { bar: 2, baz: [4, 5, 6] },
];

const obj = foo.find((f) => f.bar == 1);
if (obj) {
  obj.baz = [2, 3, 4];
}

console.log(foo);

to define the foo array.

Then we call foo.find with a callback to return the object in foo with bar set to 1.

If obj isn’t undefined, then we set obj.baz to a new value.

Conclusion

To modify object property in an array of objects with JavaScript, we can use the array find method.

Categories
JavaScript Answers

How to upload image into HTML5 canvas with JavaScript?

Sometimes, we want to upload image into HTML5 canvas with JavaScript.

In this article, we’ll look at how to upload image into HTML5 canvas with JavaScript.

How to upload image into HTML5 canvas with JavaScript?

To upload image into HTML5 canvas with JavaScript, we can get the image from the file input and draw it in the canvas.

For instance, we write

<input type="file" id="inp" /> <canvas id="canvas"></canvas>

to add the file input and the canvas.

Then we write

const draw = (e) => {
  const canvas = document.getElementById("canvas");
  canvas.width = e.target.width;
  canvas.height = e.target.height;
  const ctx = canvas.getContext("2d");
  ctx.drawImage(e.target, 0, 0);
};

document.getElementById("inp").onchange = (e) => {
  const img = new Image();
  img.onload = draw;
  img.src = URL.createObjectURL(e.target.files[0]);
};

to select the file input with getElementById.

And we set its onchange property to a function that runs when we select a file.

In it, we create an Image object and set img.onload to draw.

Then we set img.src to the base64 URL of the selected image file.

draw is called when the image is loaded.

In draw, we get the canvas with getElementById.

And we set the canvas dimensions to the image dimensions with

canvas.width = e.target.width;
canvas.height = e.target.height;

e.target is the img element.

Next, we get the canvas with getContext.

And we call drawImage to draw the image onto the canvas.

Categories
JavaScript Answers

How to combine members of multiple types in a TypeScript annotation?

Sometimes, we want to combine members of multiple types in a TypeScript annotation.

In this article, we’ll look at how to combine members of multiple types in a TypeScript annotation.

How to combine members of multiple types in a TypeScript annotation?

To combine members of multiple types in a TypeScript annotation, we can create intersection types.

For instance, we write

interface ClientRequest {
  userId: number;
  sessionKey: string;
}

interface Coords {
  lat: number;
  long: number;
}

type Combined = ClientRequest & Coords;

to combine the properties of the ClientRequest and Coords into one type with ClientRequest & Coords.

Then Combined would have the properties of both interfaces in a single type.

Conclusion

To combine members of multiple types in a TypeScript annotation, we can create intersection types.

Categories
JavaScript Answers

How to loop through children objects in JavaScript?

Sometimes, we want to loop through children objects in JavaScript.

In this article, we’ll look at how to loop through children objects in JavaScript.

How to loop through children objects in JavaScript?

To loop through children objects in JavaScript, we can use the spread operator and the forEach method.

For instance, we write

const listItems = document.querySelector("ul").children;
const listArray = [...listItems];
listArray.forEach((item) => {
  console.log(item);
});

to select all children from the ul with querySelector and children.

And then we convert the listItem node list to an array with the spread operator.

Then we call listArray.forEach with a callback to loop through each entry.

Conclusion

To loop through children objects in JavaScript, we can use the spread operator and the forEach method.