Found 9150 Articles for Object Oriented Programming

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan
Updated on 18-Aug-2020 07:00:57

355 Views

Here is our sample array of object, consider each object as representing some page of a multipage website, each object have a next property (unless it represents the last page) that points to some id of another object and a previous property (unless it represents the first page) that points to some id of its previous object.These objects are all positioned randomly right now, our job is to sort them to their correct position −let arr = [    { id: "1325asdfasdasd", next: "5345341fgdfgdd", previous:"545234123fsdfd" },    { id: "das987as9dya8s", next: "3j12k3b1231jkj" },    { id: "89ad8sasds9d8s", previous: "1j3b12k3jbasdd" }, ... Read More

Sorting arrays by two criteria in JavaScript

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

3K+ Views

Let the following be the array to be sorted by date and isImportant. All the objects with isImportant property true rank higher than any any of the object with isImportant false and both the groups sorted according to the date property.Following is our array −const array = [{    id: 545,    date: 591020824000,    isImportant: false, }, {    id: 322,    date: 591080224000,    isImportant: false, }, {    id: 543,    bdate: 591080424000,    isImportant: true, }, {    id: 423,    date: 591080225525,    isImportant: false, }, {    id: 135,    date: 591020225525,    isImportant: ... Read More

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

223 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

354 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

165 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

255 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

269 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

463 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

Advertisements