Found 9309 Articles for Object Oriented Programming

Append the current array with the squares of corresponding elements of the array in JavaScript

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

75 Views

We have an array of Numbers like this −const arr = [12, 19, 5, 7, 9, 11, 21, 4];We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array.For this sample array, the output should be −[12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16]Exampleconst arr = [12, 19, 5, 7, 9, 11, 21, 4]; const multiplyArray = (arr) => {    return arr.reduce((acc, val) => {       return acc.concat(val * val);    }, arr); }; console.log(multiplyArray(arr));OutputThe output in the console will be −[    12, 19, 5, 7, 9, 11,    21, 4, 144, 361, 25, 49,    81, 121, 441, 16 ]

Convert JavaScript array iteration result into a single line text string

AmitDiwan
Updated on 21-Aug-2020 14:16:44

210 Views

Let’s say, we have a string and an array −const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];We have to write a function that maps the array to a string containing only true and false depending on the fact whether the corresponding array element is present in the string or not.Exampleconst textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; const includesString = (arr, str) => {    return arr.reduce((acc, val) ... Read More

Inverse operation in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:13:52

784 Views

Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.Let’s write the code for this function −Exampleconst num = '1101'; const n = '11010111'; const inverseBinary = (binary) => {    return binary.split("").map(el => {       return `${1- parseInt(el, 10)}`    }).join(""); }; console.log(inverseBinary(num)); console.log(inverseBinary(n));OutputThe output in the console will be −0010 00101000

Merge objects in array with similar key JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:10:14

1K+ Views

Let’s say, we have the following array of objects −const arr = [    {id: 1, h1: 'Daily tests'},    {id: 2, h1: 'Details'},    {id: 1, h2: 'Daily classes'},    {id: 3, h2: 'Results'},    {id: 2, h3: 'Admissions'},    {id: 1, h4: 'Students'},    {id: 2, h5: 'Alumni'},    {id: 3, h3: 'Appreciations'},    {id: 1, h5: 'Tiny Tots'},    {id: 1, h6: 'Extras'}, ];We have to write a function that converts this array into an array where all the headings (h1, h2, h3 …) that have the same id get clubbed inside the same object. Therefore, let’s ... Read More

Order an array of words based on another array of words JavaScript

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

273 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

How to Sort object of objects by its key value JavaScript

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

528 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

How to combine 2 arrays into 1 object in JavaScript

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

645 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

163 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 JavaScript

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

267 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

How to splice duplicate item in array JavaScript

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

601 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 ]

Advertisements