Found 9326 Articles for Object Oriented Programming

Take in array of objects and convert the above JSON to a Tree-structure in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:46:34

1K+ Views

Suppose, we have an array of objects like this −const arr = [    {       "parentIndex": '0' ,       "childIndex": '3' ,       "parent": "ROOT",       "child": "root3"    },    {       "parentIndex": '3' ,       "childIndex": '2' ,       "parent": "root3" ,       "child": "root2"    },    {       "parentIndex": '3' ,       "childIndex": '1' ,       "parent": "root3" ,       "child": "root1"    } ];We are required to write a JavaScript ... Read More

Merge arrays in column wise to another array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:43:15

602 Views

Suppose, we have three arrays of numbers like these −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5];We are required to write a JavaScript function that takes in three such arrays. The function should then construct an array of objects based on these three arrays like this −const output = [    {"code": 123, "year": 2013, "period": 3},    {"code": 456, "year": 2014, "period": 4},    {"code": 789, "year": 2015, "period": 5} ];ExampleThe code for this will be −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const ... Read More

Loop through an index of an array to search for a certain letter in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:42:19

860 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and a single character as the second argument.The function should return true if the character specified by second argument exists in any of the strings of the array, false otherwise.ExampleThe code for this will be −const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(!el.includes(letter)){          continue;       ... Read More

Count by unique key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:40:57

523 Views

Suppose, we have an array of objects like this −const arr = [    {       assigned_user:{          name:'Paul',          id: 34158       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    } ];We are required ... Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:38:41

400 Views

We are required to write a JavaScript program that provides the user an input to enter a string value.The program should then check the input value against some hard-coded array values. Our program should print true to the screen if the input string value is included in the array, false otherwise.ExampleThe code for this will be −            CHECK EXISTENCE           const arr = ['arsenal', 'chelsea', 'everton', 'fulham',       'swansea'];       const checkExistence = () => {          const userInput = document.getElementById("input").value;          const exists = arr.includes(userInput);          document.getElementById('result').innerText = exists;       };            Check     OutputAnd the output on the screen will be −

Reduce an array to groups in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:37:04

161 Views

Suppose, we have an array of strings that contains some duplicate entries like this −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green'];We are required to write a JavaScript function that takes in one such array. The function should merge all the duplicate entries with one another.Therefore, the output for the above input should look like this −const output = ['blueblue', 'green', 'blue', 'yellowyellow', 'green'];ExampleThe code for this will be −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; const combineDuplicate = (arr = []) => {    let prev = null;    const groups = arr.reduce((acc, value) ... Read More

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:35:51

813 Views

Suppose, we have an array of objects like this −const arr = [{    name: 'Paul',    country: 'Canada', }, {    name: 'Lea',    country: 'Italy', }, {    name: 'John',    country: 'Italy', }, ];We are required to devise a way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object.For instance −When we type "lea", we want to go through all the objects and all their properties to return the objects that contain "lea". When we type "italy", we want to go through all ... Read More

Remove duplicates and map an array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:32:37

501 Views

Suppose, we have an array of objects like this −const arr = [    {id:123, value:"value1", name:"Name1"},    {id:124, value:"value2", name:"Name1"},    {id:125, value:"value3", name:"Name2"},    {id:126, value:"value4", name:"Name2"} ];Note that some of the "name" property in objects within the array are duplicate.We are required to write a JavaScript function that takes in one such array of objects. The function should then construct a new array of strings that contains only unique "name" property value from the array.Therefore, the output for the above input should look like this −const output = ["Name1", "Name2"];ExampleThe code for this will be −const arr ... Read More

How to group an array of objects by key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:31:17

887 Views

Suppose, we have an array of objects containing data about some cars like this −const arr = [    {       'make': 'audi',       'model': 'r8',       'year': '2012'    }, {       'make': 'audi',       'model': 'rs5',       'year': '2013'    }, {       'make': 'ford',       'model': 'mustang',       'year': '2012'    }, {       'make': 'ford',       'model': 'fusion',       'year': '2015'    }, {       'make': 'kia',       ... Read More

Finding day of week from date (day, month, year) in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:28:11

556 Views

We are required to write a JavaScript function that takes in three argument, namely:day, month and year. Based on these three inputs, our function should find the day of the week on that date.For example: If the inputs are −day = 15, month = 8, year = 1993OutputThen the output should be −const output = 'Sunday'ExampleThe code for this will be −const dayOfTheWeek = (day, month, year) => {    // JS months start at 0    return dayOfTheWeekJS(day, month - 1, year); } function dayOfTheWeekJS(day, month, year) {    const DAYS = [       'Sunday',     ... Read More

Advertisements