Object Oriented Programming Articles - Page 74 of 579

Efficient algorithm for grouping elements and counting duplicates in JavaScript

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

378 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

176 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

JavaScript union of two objects

Alshifa Hasnain
Updated on 26-Feb-2025 19:36:58

745 Views

In this article, we will learn to calculate the union of two objects in JavaScript. The union of two objects results in a new object that contains all the properties of both objects. What is the Union of Two Objects? The union of two objects is the process of combining the properties of both objects into a single object. For example Input − const obj1 = { name: " ", email: " " }; const obj2 = { name: ['x'], email: ['y']}; Output − const output = { name: {" ", [x]}, email: {" ", [y]} }; Different Approaches ... Read More

Mobile

Create an object based on 2 others in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:33:32

154 Views

Suppose, we have two JavaScript objects defined like this −const a = {    a: 1,    af: function() { console.log(this.a) }, }; const b = {    b: 2,    bf: function() { console.log(this.b) }, };We are required to write a JavaScript function that takes in two such objects. Create another object which will get the properties of a and b, like this −const output = {    a: 1,    af: function() { console.log(this.a) },    b: 2,    bf: function() { console.log(this.b) }, }Note that a and b need to stay the same.ExampleThe code for this will ... Read More

Return the greatest possible product of n numbers from the array in JavaScript

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

139 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument.Our function should calculate and return the greatest possible product of n numbers from the array.ExampleThe code for this will be −const getHighestProduct = (arr, num) => {    let prod = 1;    const sorter = (a, b) => a - b;    arr.sort(sorter);    if (num > arr.length || num & 2 && arr[arr.length - 1] < 0) {       return;    };    if (num % 2) { ... Read More

Nested collection filter with JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:28:17

1K+ Views

Suppose, we have an array of nested objects like this −const arr = [{    id: 1,    legs:[{       carrierName:'Pegasus'    }] }, {    id: 2,    legs:[{       carrierName: 'SunExpress'    },    {       carrierName: 'SunExpress'    }] }, {    id: 3,    legs:[{       carrierName: 'Pegasus'    },    {       carrierName: 'SunExpress'    }] }];We are required to write a JavaScript function that takes one such array as the first argument and a search query string as the second argument.Our function should filter ... Read More

Build tree array from JSON in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:25:34

3K+ Views

Suppose, we have the following array in JavaScript −const arr = [{    "code": "2",    "name": "PENDING" }, {    "code": "2.2",    "name": "PENDING CHILDREN" }, {    "code": "2.2.01.01",    "name": "PENDING CHILDREN CHILDREN" }, {    "code": "2.2.01.02",    "name": "PENDING CHILDREN CHILDREN02" }, {    "code": "1",    "name": "ACTIVE" }, {    "code": "1.1",    "name": "ACTIVE CHILDREN" }, {    "code": "1.1.01",    "name": "ACTIVE CHILDREN CHILDREN" }];We are required to write a JavaScript function that takes in one such array. The function should build a tree structure from this array based on ... Read More

Sum identical elements within one array in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:18:54

313 Views

We are required to write a JavaScript function that takes in an array of Numbers.The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed.ExampleThe code for this will be −const arr = [20, 20, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => {    let map = {};    for (let i = 0; i < arr.length; i++) {       let el = arr[i];       map[el] = map[el] ? map[el] + 1 : 1;    };    const res = [];    for (let count in map) {       res.push(map[count] * count);    };    return res; }; console.log(sumIdentical(arr));OutputAnd the output in the console will be −[ 1, 5, 20, 60 ]

Grouping an Array and Counting items creating new array based on Groups in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:17:54

390 Views

Suppose, we have an array of objects like this −const arr = [    { region: "Africa", fruit: "Orange", user: "Gary" },    { region: "Africa", fruit: "Apple", user: "Steve" },    { region: "Europe", fruit: "Orange", user: "John" },    { region: "Europe", fruit: "Apple", user: "bob" },    { region: "Asia", fruit: "Orange", user: "Ian" },    { region: "Asia", fruit: "Apple", user: "Angelo" },    { region: "Africa", fruit: "Orange", user: "Gary" } ];We are required to write a JavaScript function that takes in one such array. The function should prepare a new array of objects that ... Read More

Advertisements