Found 10483 Articles for Web Development

How do I search through an array using a string, which is split into an array with JavaScript?

AmitDiwan
Updated on 18-Aug-2020 06:56:19

226 Views

We are given an array of strings and another string for which we are required to search in the array. We can filter the array checking whether it contains all the characters that user provided through the input.The code for doing the same would be −ExampleSolution 1const deliveries = ["14/02/2020, 11:47, G12, Kalkaji", "13/02/2020, 11:48, A59, Amar Colony"]; const input = "g12, kal"; const pn = input.split(" "); const requiredDeliveries = deliveries.filter(delivery => pn.every(p => delivery.toLowerCase() .includes(p.toLowerCase()))); console.log(requiredDeliveries);OutputThe output in console −["14/02/2020, 11:47, G12, Kalkaji"]In another and a bit better approach we can remove the step of splitting the input ... Read More

Is it possible to have JavaScript split() start at index 1?

AmitDiwan
Updated on 18-Aug-2020 06:55:16

355 Views

As of the official String.prototype.split() method there exist no way to start splitting a string from index 1 or for general from any index n, but with a little tweak in the way we use split(), we can achieve this functionality.We followed the following approach −We will create two arrays −One that is splitted from 0 to end --- ACTUALSecond that is splitted from 0 TO STARTPOSITION --- LEFTOVERNow, we iterate over each element of leftover and splice it from the actual array. Thus, the actual array hypothetically gets splitted from STARTINDEX to END.Exampleconst string = 'The quick brown fox ... Read More

Flat a JavaScript array of objects into an object

AmitDiwan
Updated on 18-Aug-2020 06:54:08

2K+ Views

To flat a JavaScript array of objects into an object, we created a function that takes array of object as only argument. It returns a flattened object with key append by its index. The time complexity is O(mn) where n is the size of array and m is the number of properties in each object. However, its space complexity is O(n) where n is the size of actual array.Example//code to flatten array of objects into an object //example array of objects const notes = [{    title: 'Hello world',    id: 1 }, {    title: 'Grab a coffee',   ... Read More

Asynchronous Functions and the Node Event Loop in Javascript

sudhir sharma
Updated on 06-Aug-2020 07:54:48

167 Views

Asynchronous Functions, the programs continue to run. It does not wait! This way the waiting time of the user is reduced. Also, Javascript as a programming language itself is asynchronous.For example, if in code we are running an expensive request, which might require a lot of time, then in case of an asynchronous function, the waiting time would be too much, and the user wouldn’t be able to perform anything else too!Thus generally we prefer using asynchronous code when performing expensive and time-consuming operations.Let’s take an example of Anyncronous function in javascript −Exampleconsole.log('One'); jQuery.get('page.html', function (data) {    console.log("Two"); }); console.log('Three');OutputOne, ... Read More

How to add properties and methods to an existing object in JavaScript?

AmitDiwan
Updated on 22-Jul-2020 08:04:08

257 Views

Following is the code for adding properties and methods to an existing object in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Add properties and methods to an existing object in JavaScript CLICK HERE Click on the above button to add property and methods to student object and display them    let resEle = document.querySelector(".result");    let BtnEle = ... Read More

Currying VS Partial Application in JavaScript.

AmitDiwan
Updated on 22-Jul-2020 08:02:59

272 Views

Currying − In currying a function takes another function and some arguments. The function then returns one function with one parameter only. It returns the function with one argument which can be chained together.Partial application − In partial application some of the arguments can be bind to some values to produce a function with less arguments.Following is the code for currying vs partial application in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       ... Read More

Drawing an image in canvas using in JavaScript

AmitDiwan
Updated on 22-Jul-2020 07:58:39

464 Views

Following is the code for drawing an image in canvas using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    } Drawing an image in canvas CLICK HERE Click on the above button to draw the image in canvas    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    BtnEle.addEventListener("click", () => {       var c = document.querySelector(".canvas1");       var ctx = c.getContext("2d");       var img = document.querySelector(".flower");       ctx.drawImage(img, 10, 10);    }); OutputOn clicking the ‘CLICK HERE’ button −

JavaScript program to convert 24 hours format to 12 hours

Ravi Ranjan
Updated on 06-Jun-2025 19:13:26

2K+ Views

To convert 24 hours format to 12 hours in Javascript, we have discussed two different approaches. In this article we are having time in 24 hours format, our task is to write a JavaScript program to convert 24 hours format to 12 hours. Approaches to Convert 24-hours format to 12-hours Here is a list of approaches to write a JavaScript program to convert 24 hours format to 12 hours which we will be discussing in this article with stepwise explanation and complete example codes. Using if-else Statement Using Date Object ... Read More

First class function in JavaScript

AmitDiwan
Updated on 22-Jul-2020 07:53:06

668 Views

JavaScript treats functions as objects and allow us to pass functions as parameter to another function and even return functions from other functions. In JavaScript the functions are first class functions meaning we can store them in variable, objects and array. The higher order functions can take function, return them or do both.Following is the code to implement first class functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 18px;       font-weight: ... Read More

Identifying False values in JavaScript

AmitDiwan
Updated on 22-Jul-2020 07:51:36

527 Views

JavaScript has the following values evaluate to false when evaluated as Boolean type − false0empty string: "" , '' , or ``nullundefinedNaN — not a number valueFollowing is the code for identifying falsy values in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result{       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Identifying Falsy values in JavaScript. CLICK HERE Click on the above button to identify some of the ... Read More

Advertisements