Found 9321 Articles for Object Oriented Programming

Join two objects by key in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:31:41

685 Views

Suppose, we have two child and parent JSON arrays of objects like these −const child = [{    id: 1,    name: 'somename',    parent: {       id: 2    }, }, {    id: 2,    name: 'some child name',    parent: {       id: 4    } }]; const parent = [{    id: 1,    parentName: 'The first',    child: {} }, {    id: 2,    parentName: 'The second',    child: {} }, {    id: 3,    parentName: 'The third',    child: {} }, {    id: 4,    parentName: 'The ... Read More

Form a sequence out of an array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:28:25

690 Views

Suppose we have a sorted array of numbers like this where we can have consecutive numbers.const arr = [1, 2, 3, 5, 7, 8, 9, 11];We are required to write a JavaScript function that takes one such array.Our function should form a sequence for this array. The sequence should be such that for all the consecutive elements of the array, we have to just write the starting and ending numbers replacing the numbers in between with a dash (-), and keeping all other numbers unchanged.Therefore, for the above array, the output should look like −const output = '1-3, 5, 7-9, ... Read More

Array of objects to array of arrays in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:25:40

726 Views

Suppose, we have an array of objects like this −const arr = [    {"Date":"2014", "Amount1":90, "Amount2":800},    {"Date":"2015", "Amount1":110, "Amount2":300},    {"Date":"2016", "Amount1":3000, "Amount2":500} ];We are required to write a JavaScript function that takes in one such array and maps this array to another array that contains arrays instead of objects.Therefore, the final array should look like this −const output = [    ['2014', 90, 800],    ['2015', 110, 300],    ['2016', 3000, 500] ];ExampleThe code for this will be −const arr = [    {"Date":"2014", "Amount1":90, "Amount2":800},    {"Date":"2015", "Amount1":110, "Amount2":300},    {"Date":"2016", "Amount1":3000, "Amount2":500} ]; const arrify ... Read More

Trim and split string to form array in JavaScript

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

738 Views

Suppose, we have a comma-separated string like this −const str = "a, b, c, d , e";We are required to write a JavaScript function that takes in one such and sheds it off all the whitespaces it contains.Then our function should split the string to form an array of literals and return that array.ExampleThe code for this will be −const str = "a, b, c, d , e"; const shedAndSplit = (str = '') => {    const removeSpaces = () => {       let res = '';       for(let i = 0; i < str.length; ... Read More

Removing duplicates and keep one instance in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:23:10

172 Views

We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => {    for(let i = 0; i < arr.length; ){       const el = arr[i];       if(i !== arr.lastIndexOf(el)){          arr.splice(i, 1);       }       else{          i++;       };    }; }; deleteDuplicate(arr); console.log(arr);OutputAnd the output in the console will be −[ 7, 1, 6, 4, 5, 8 ]

JavaScript - Merge two arrays according to id property

AmitDiwan
Updated on 23-Nov-2020 10:21:46

4K+ Views

Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [    {"id":"123", "name":"name 1"},    {"id":"456", "name":"name 2"} ]; const arr2 = [    {"id":"123", "address":"address 1"},    {"id":"456", "address":"address 2"} ];We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.The third array should contain the user id, name, and address object of corresponding users.ExampleThe code for this will be −const ... Read More

Elements that appear twice in array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:19:44

474 Views

We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => {    let count = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(num !== el){          continue;       };   ... Read More

How to merge two different array of objects using JavaScript?

AmitDiwan
Updated on 23-Nov-2020 10:16:52

407 Views

Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[    { PersonalID: '11', qusetionNumber: '1', value: 'Something' },    { PersonalID: '12', qusetionNumber: '2', value: 'whatever' },    { PersonalID: '13', qusetionNumber: '3', value: 'anything' },    { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[    { qusetionNumber: '2', chID: '111', cValue: 'red' },    { qusetionNumber: '2', chID: '112', cValue: 'green'},    { qusetionNumber: '2', chID: '113', cValue: 'blue' },    {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'},    {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];We ... Read More

How to do multidimensional array intersection using JavaScript?

AmitDiwan
Updated on 23-Nov-2020 10:13:40

314 Views

We are required to write a JavaScript function that takes in a multidimensional array of arrays of literal values. Our function should return the intersecting array of all the subarrays present in the input array.ExampleThe code for this will be −const arr = [    ["garden","canons","philips","universal"],    ["universal","ola","uber","bangalore"] ]; const findMultiIntersection = (arr = []) => {    const res = [];    arr.forEach(el => {       const thisObj = this;       el.forEach(element => {          if(!thisObj[element]){             thisObj[element] = true;          }          else{             res.push(element)          };       });    }, {});    return res; }; console.log(findMultiIntersection(arr));OutputAnd the output in the console will be −[ 'universal' ]

How to dynamically combine all provided arrays using JavaScript?

AmitDiwan
Updated on 23-Nov-2020 10:12:33

388 Views

Suppose we have two arrays of literals like these −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f'];We are required to write a JavaScript function that takes in two such arrays and build all possible combinations from the arrays.So, for these two arrays, the output should look like −const output = [ad, ae, af, bd, be, bf, cd, ce, cf];ExampleThe code for this will be −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f']; const combineArrays = (...arr) => {    const res = [];    const combinePart = (part, index) => {       arr[index].forEach(el => ... Read More

Advertisements