AmitDiwan has Published 10744 Articles

Filtering of JavaScript object

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:21:11

241 Views

Here we need to create a function that takes in an object and a search string and filters the object keys that start with the search string and returns the objectHere is the code for doing so −Exampleconst obj = {    "PHY": "Physics",    "MAT": "Mathematics",    "BIO": "Biology", ... Read More

Display array items on a div element on click of button using vanilla JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:19:57

4K+ Views

To embed the elements of an array inside a div, we just need to iterate over the array and keep appending the element to the divThis can be done like this −Exampleconst myArray = ["stone", "paper", "scissors"]; const embedElements = () => {    myArray.forEach(element => {       ... Read More

Combine objects and delete a property with JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:17:45

223 Views

We have the following array of objects that contains two objects and we are required to combine both objects into one and get rid of the chk property altogether −const err = [    {       "chk" : true,       "name": "test"    },    { ... Read More

Clearing localStorage in JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:16:30

489 Views

There exists two ways actually to clear the localStorage via JavaScript.Way1 − Using the clear() methodlocalStorage.clear();Way2 − Iterating over localStorage and deleting all keyfor(key in localStorage){ delete localStorage[key]; }Both ways will work.Example if (typeof(Storage) !== "undefined") {    localStorage.setItem("product", "Tutorix");    // cleared before displaying    localStorage.clear(); ... Read More

Why does Array.map(Number) convert empty spaces to zeros? JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:14:05

403 Views

Let’s say we are given the following code and output and we need to figure out why JavaScript converts empty strings(“ “) to 0 −const digify = (str) => {    const parsedStr = [...str].map(Number)    return parsedStr; } console.log(digify("778 858 7577"))Output[ 7, 7, 8, 0, 8, 5, 8, 0, ... Read More

How to set attribute in loop from array JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:12:47

1K+ Views

Let’s say we are required to write a function that takes in an array and changes the id attribute of first n divs present in a particular DOM according to corresponding values of this array, where n is the length of the array.We will first select all divs present in ... Read More

Get filename from string path in JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:12:00

2K+ Views

We need to write a function that takes in a string file path and returns the filename. Filename usually lives right at the very end of any path, although we can solve this problem using regex but there exists a simpler one-line solution to it using the string split() method ... Read More

How to convert array to object in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:11:12

379 Views

Let’s say we need to convert the following array of array into array of objects with keys as English alphabetconst data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];This can be done by mapping over the actual arrays and reducing the subarrays into objects like ... Read More

Replace() with Split() in JavaScript to append 0 if number after comma is a single digit

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:10:23

294 Views

Let’s say our sample string is −const a = "250, 5";If the number after “, ” is a single digit we have to append a 0 to it, If the string contains more than one ‘, ’, we have to return -1This can be done simply by combining the split() ... Read More

Join every element of an array with a specific character using for loop in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:09:13

277 Views

Here we are supposed to write a function that takes in two arguments, first an array of String or Number literals, second a String and we have to return a string that contains all the elements of the array prepended and appended by the string.For example −applyText([1, 2, 3, 4], ... Read More

Advertisements