Order an Array of Words Based on Another Array in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:07:19

400 Views

Let’s say, we have the following array of objects sorted according to its id property −const unordered = [{    id: 1,    string: 'sometimes' }, {    id: 2,    string: 'be' }, {    id: 3,    string: 'can' }, {    id: 4,    string: 'life' }, {    id: 5,    string: 'tough' }, {    id: 6,    string: 'very' }, ];And another array of string like this −const ordered = ['Life', 'sometimes', 'can', 'be', 'very', 'tough'];We have to sort the first array so its string property have the same order of string as it ... Read More

Sort Object of Objects by Key Value in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:55:27

654 Views

Let’s say, we have an object with keys as string literals and their values as objects as well like this −const companies = {    'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'},    'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'},    'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'},    'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'},    'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'},    'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'},    'dis n dat': {employees: 540, worth: '1m', CEO: 'Mohit Sharma'}, }We are required to write ... Read More

Combine 2 Arrays into 1 Object in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:51:33

786 Views

Let’s say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value.We will reduce the first array, at the same time accessing elements of the second array by index. The code for this will be −Exampleconst keys = [    'firstName',    'lastName',    'isEmployed',    'occupation',    'address',    'salary',    'expenditure' ]; const values = [    'Hitesh',    'Kumar',    false, ... Read More

Make Numbers in Array Relative to 0-100 in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:49:40

264 Views

Let’s say, we have an array that contains some numbers, our job is to write a function that takes in the array and maps all the values relative to 0 to 100. Means that the greatest number should get replaced by 100, the smallest by 100 and all others should get converted to specific numbers between 0 and 100 according to the ratio.Following is the code for doing the same −Exampleconst numbers = [45.71, 49.53, 18.5, 8.38, 38.43, 28.44]; const mapNumbers = (arr) => {    const max = Math.max(...arr);    const min = Math.min(...arr);    const diff = max ... Read More

Positive Integer Square Array in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:46:49

356 Views

Let’s say, we have an array that contains some numbers, positive, negative, decimals and integers. We have to write a function that takes in an array and returns an array of square of all the positive integers from the original array.Let’s write the code for this function −Exampleconst arr = [1, -4, 6.1, 0.1, 2.6, 5, -2, 1.9, 6, 8.75, -7, 5]; const squareSum = (arr) => {    return arr.reduce((acc, val) => {       //first condition checks for positivity and second for wholeness of the number       if(val > 0 && val % 1 === 0){          acc += val*val;       };       return acc;    },0); } console.log(squareSum(arr));OutputThe output in the console will be −87

Splice Duplicate Items in Array using JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:42:16

689 Views

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else.We will use the Array.prototype.splice() method to remove entries inplace, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element.Exampleconst arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5]; arr.forEach((el, ind, array) => {    if(array.indexOf(el) !== array.lastIndexOf(el)){       array.splice(ind, 1);    } }); console.log(arr);OutputThe output in the console will be −[    4, 1, 5, 2,    6, 8, 7 ]

Sum from Array Values with Similar Key in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:34:47

568 Views

Let’s say, here is an array that contains some data about the stocks sold and purchased by some company over a period of time.const transactions = [    ['AAPL', 'buy', 100],    ['WMT', 'sell', 75],    ['MCD', 'buy', 125],    ['GOOG', 'sell', 10],    ['AAPL', 'buy', 100],    ['AAPL', 'sell', 100],    ['AAPL', 'sell', 20],    ['DIS', 'buy', 15],    ['MCD', 'buy', 10],    ['WMT', 'buy', 50],    ['MCD', 'sell', 90] ];We want to write a function that takes in this data and returns an object of arrays with key as stock name (like ‘AAPL’, ‘MCD’) and value as array ... Read More

Fill Missing Numeric Values in a JavaScript Array

AmitDiwan
Updated on 21-Aug-2020 13:28:23

539 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this −const arr = [null, null, -1, null, null, null, -3, null, null, null];We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression.About Arithmetic ProgressionA series / array of numbers is said to ... Read More

Returning an Array with Minimum and Maximum Elements in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:22:00

173 Views

Provided an array of numbers, let’s say −const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76];We are required to write a function that picks the minimum and maximum element from the array and returns an array of those two numbers with minimum at index 0 and maximum at 1.We will use the Array.prototype.reduce() method to build a minimum maximum array like this −Exampleconst arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; const minMax = (arr) => {    return arr.reduce((acc, val) => {       if(val < acc[0]){          acc[0] = val;       }       if(val > acc[1]){          acc[1] = val;       }       return acc;    }, [Infinity, -Infinity]); }; console.log(minMax(arr));OutputThe output in the console will be −[ 4, 545 ]

Reverse Digits of an Integer in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:18:25

419 Views

We are required to write Number.prototype.reverse() function that returns the reversed number of the number it is used with.For example −234.reverse() = 432; 6564.reverse() = 4656;Let’s write the code for this function. We will use a recursive approach like this −Exampleconst reverse = function(temp = Math.abs(this), reversed = 0, isNegative = this < 0){    if(temp){       return reverse(Math.floor(temp/10), (reversed*10)+temp%10,isNegative);    };    return !isNegative ? reversed : reversed*-1; }; Number.prototype.reverse = reverse; const n = -12763; const num = 43435; console.log(num.reverse()); console.log(n.reverse());OutputThe output in the console will be −53434 -36721

Advertisements