Categories
JavaScript Answers

How to sort an associative array by its values in JavaScript?

Sometimes, we want to sort an associative array by its values in JavaScript.

In this article, we’ll look at how to sort an associative array by its values in JavaScript.

How to sort an associative array by its values in JavaScript?

To sort an associative array by its values in JavaScript, we use some object methods.

For instance, we write

const sortedObj = Object.fromEntries(
  Object.entries(data).sort(([, val1], [, val2]) => val1 - val2)
);

to call Object.entries with data to return an array with the key-value pairs in their own arrays.

Then we call sort with a callback that gets the property values from each array and compare them to do the sorting and return the sorted array.

Then we call Object.fromEntries to convert the array of key-value pair arrays back into an object.

Conclusion

To sort an associative array by its values in JavaScript, we use some object methods.

Categories
JavaScript Answers

How to populate drop down list with array with JavaScript?

Sometimes, we want to populate drop down list with array with JavaScript.

In this article, we’ll look at how to populate drop down list with array with JavaScript.

How to populate drop down list with array with JavaScript?

To populate drop down list with array with JavaScript, we call the createElement method.

For instance, we write

const arr = ["1", "2", "3", "4"];
arr.forEach((item) => {
  const optionObj = document.createElement("option");
  optionObj.textContent = item;
  document.getElementById("myselect").appendChild(optionObj);
});

to call arr.forEach with a callback that calls createElement to create an option element.

Then we set the text of the option by setting the textContent property.

Then we select the select element and append the option element to it with appendChild.

Conclusion

To populate drop down list with array with JavaScript, we call the createElement method.

Categories
JavaScript Answers

How to detect if argument is array instead of object with Node.js?

Sometimes, we want to detect if argument is array instead of object with Node.js.

In this article, we’ll look at how to detect if argument is array instead of object with Node.js.

How to detect if argument is array instead of object with Node.js?

To detect if argument is array instead of object with Node.js, we use the util.isArray method.

For instance, we write

const util = require("util");

const isArray = util.isArray([]);

to call util.isArray with an empty array to check if the argument is an array.

true is returned if it is.

Conclusion

To detect if argument is array instead of object with Node.js, we use the util.isArray method.

Categories
JavaScript Answers

How to put an image file in a JSON object with JavaScript?

Sometimes, we want to put an image file in a JSON object with JavaScript.

In this article, we’ll look at how to put an image file in a JSON object with JavaScript.

How to put an image file in a JSON object with JavaScript?

To put an image file in a JSON object with JavaScript, we convert it to a data URI.

For instance, we write

const convertToDataURLviaCanvas = (url, callback, outputFormat) => {
  const img = new Image();
  img.crossOrigin = "Anonymous";
  img.onload = () => {
    const canvas = document.createElement("CANVAS");
    const ctx = canvas.getContext("2d");
    canvas.height = img.height;
    canvas.width = img.width;
    ctx.drawImage(img, 0, 0);
    const dataURL = canvas.toDataURL(outputFormat);
    callback(dataURL);
  };
  img.src = url;
};

convertToDataURLviaCanvas("http://bit.ly/18g0VNp", (base64Img) => {
  //...
});

to define the convertToDataURLviaCanvas function.

In it, we create an img element with the Image constructor.

Then we set its onload property to a function that creates a canvas with createElement.

Then we call getContext to get the context object.

We call drawImage to draw the image onto the canvas.

And we get the base64 data URI with toDataURL.

We then load the image with the url by setting the src property.

Next, we call convertToDataURLviaCanvas with the image URL and a callback that has the base 64 URI string in the base64Img parameter.

Conclusion

To put an image file in a JSON object with JavaScript, we convert it to a data URI.

Categories
JavaScript Answers

How to get range of items with JavaScript array?

Sometimes, we want to get range of items with JavaScript array.

In this article, we’ll look at how to get range of items with JavaScript array.

How to get range of items with JavaScript array?

To get range of items with JavaScript array, we use the slice method.

For instance, we write

const fruits = ["apple", "banana", "peach", "plum", "pear"];
const slice1 = fruits.slice(1, 3);

to call fruits.slice with index 1 and 3 to return an array with the items in fruits between indexes 1 and 2.

Conclusion

To get range of items with JavaScript array, we use the slice method.