Found 9326 Articles for Object Oriented Programming

Alphanumeric sorting using JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:58:22

234 Views

We have a mixed array that we need to sort by alphabet and then by digit −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105'];ExampleThe code for this will be −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; const alphaNumericSort = (arr = []) => {    arr.sort((a, b) => {       const aPart = a.split('-');       const bPart = b.split('-');       return aPart[0].toLowerCase().localeCompare(bPart[0].toLowerCase()) || aPart[1] - bPart[1];    }); }; alphaNumericSort(arr); console.log(arr);OutputAnd the output in the console will be −[    'Ab-1', 'ab-2',    'ab-3', 'ab-10', ... Read More

Turning a 2D array into a sparse array of arrays in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:57:14

157 Views

Suppose, we have a 2-D array like this −const arr = [    [3, 1],    [2, 12],    [3, 3] ];We are required to write a JavaScript function that takes in one such array.The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array.Therefore, for the input array, output[3][1] = 1; output[2][12] = 1; output[3][3] = 1;And rest all elements should be initialized to undefinedTherefore, the final output should look like −const output = [    undefined,    undefined,    [       ... Read More

Sort by index of an array in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:55:30

2K+ Views

Suppose we have the following array of objects −const arr = [    {       'name' : 'd',       'index' : 3    },    {       'name' : 'c',       'index' : 2    },    {       'name' : 'a',       'index' : 0    },    {       'name' : 'b',       'index' : 1    } ];We are required to write a JavaScript function that takes in one such array.The function should sort this array in increasing order according to ... Read More

Sorting an array by price in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:52:50

1K+ Views

Suppose we have an array of objects that contains data about some houses and price like this −const arr = [    {       "h_id": "3",       "city": "Dallas",       "state": "TX",       "zip": "75201",       "price": "162500"    },    {       "h_id": "4",       "city": "Bevery Hills",       "state": "CA",       "zip": "90210",       "price": "319250"    },    {       "h_id": "5",       "city": "New York",       "state": "NY",     ... Read More

Returning the highest value from an array in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:48:01

210 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element.ExampleThe code for this will be −const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => {    let greatest = -Infinity;    if(!arr?.length){       return null;    };    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el < greatest){          continue;       };       greatest = el;    };    return greatest; }; console.log(findGreatest(arr));OutputAnd the output in the console will be −20

Get the smallest array from an array of arrays in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:46:46

418 Views

Suppose, we have a nested array of arrays like this −const arr = [    ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],    ["RIGHT", "LEFT", "TOP"],    ["TOP", "LEFT"] ];We are required to write a JavaScript function that takes in one such array. The function then should pick the smallest subarray (smallest in sense of a number of elements contained) and return it.ExampleThe code for this will be −const arr = [    ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],    ["RIGHT", "LEFT", "TOP"],    ["TOP", "LEFT"] ]; const findShortest = (arr = []) => {    const res = arr.reduce((acc, val, ind) => ... Read More

Most efficient method to groupby on an array of objects - JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:45:05

196 Views

Suppose, we have an array of objects like this −const arr = [    { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },    { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },    { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },    { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },    { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },    { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },    { Phase: ... Read More

Efficient algorithm for grouping elements and counting duplicates in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:40:42

261 Views

We have got an array of objects. If one property of the object is the same as in another object, we consider it to be a duplicate entry.We want to group objects by this property and store information about how many times the "duplicate" occurred.   X A B O    Y X Z I    Y X Z U    X A B L    Y X Z KWe want to group by the first value.Another two properties are the same in each duplicate too, but comparing the first value will be enough.We need to display to the user a ... Read More

Find all disjointed intersections in a set of vertical line segments in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:38:22

97 Views

We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region.The origin of our coordinates system is the top-left corner, so y2 is always greater than y1.This is an example −const regions = [    [10, 100],    [50, 120],    [60, 180],    [140, 220] ];We are required to write a JavaScript function that takes in one such region array as the first argument and a number as the second argument.We would like to find out all disjointed intersections greater than a ... Read More

Data manipulation with JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:36:38

2K+ Views

Suppose we have two arrays describing some cashflow like these −const months = ["jan", "feb", "mar", "apr"]; const cashflows = [    {'month':'jan', 'value':10},    {'month':'mar', 'value':20} ];We are required to write a JavaScript function that takes in two such arrays. Our function should then construct a combined array of objects that contains an object for each month and the value of cash flow for that month.Therefore, for the above array, the output should look like −const output = [    {'month':'jan', 'value':10},    {'month':'feb', 'value':''},    {'month':'mar', 'value':20},    {'month':'apr', 'value':''} ];ExampleThe code for this will be −const months ... Read More

Advertisements