Found 9313 Articles for Object Oriented Programming

JavaScript get the length of select options(dropdown)?

AmitDiwan
Updated on 01-Sep-2020 10:36:50

897 Views

Let’s say the following is our dropdown (select) −    John    Mike    Sam    David    Carol Following is the code to get the length of list options in JavaScript −Example Live Demo Document John Mike Sam David Carol    var lengthOfListOptions = $("#lengthListOfOptionDemo option").length;    console.log("The length is=" + lengthOfListOptions); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Access previously iterated element within array.map in JavaScript?

AmitDiwan
Updated on 31-Aug-2020 13:52:35

106 Views

Let’s say the following is our array −var details = [    {subjectId:110, subjectName: 'Java' },    {subjectId:111, subjectName: 'Javascript' },    {subjectId:112, subjectName: 'MySQL' },    {subjectId:113, subjectName: 'MongoDB' } ];Now, use the concept of map(). The code is as follows −Examplevar details = [    {subjectId:110, subjectName: 'Java' },    {subjectId:111, subjectName: 'JavaScript' },    {subjectId:112, subjectName: 'MySQL' },    {subjectId:113, subjectName: 'MongoDB' } ]; var output = details.map((detailsObject, index) => {    var tempObject = {};    tempObject.subjectId= detailsObject.subjectId;    tempObject.subjectName = detailsObject.subjectName;    const getThePreviousObject = index != 0 ? details[index-1] : null;    tempObject.previousSubjectName = ... Read More

How to count digits of given number? JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:50:58

3K+ Views

The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it.For example −The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3Let's write the code for this function −Exampleconst num = 2353454; const digits = (num, count = 0) => {    if(num){       return digits(Math.floor(num / 10), ++count);    };    return count; }; console.log(digits(num)); console.log(digits(123456)); console.log(digits(53453)); console.log(digits(5334534534));OutputThe output in the console will be −7 6 5 10

Add two consecutive elements from the original array and display the result in a new array with JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:50:00

257 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.For example, if the input array is −const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];Then the output should be −const newArrayOne = [1, 5, 9, 13, 17]Let's write the code for this function −Exampleconst arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const doubleSum = arr => {    const res = [];    for(let i = 0; i < arr.length; i += 2){       res.push(arr[i] + (arr[i+1] || 0));    };    return res; }; console.log(doubleSum(arrayOne));OutputThe output in the console will be −[ 1, 5, 9, 13, 17 ]

How to check whether multiple values exist within a JavaScript array

AmitDiwan
Updated on 31-Aug-2020 13:47:44

947 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not.Following are our arrays −const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89];Let's write the code and check for multiple values −Exampleconst arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => {    const indexArray = first.map(el => {       return second.indexOf(el);    });    return indexArray.indexOf(-1) === -1; } console.log(contains(arr1, arr2));OutputThe output in the console will be −true

Loop backward in array of objects JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:46:30

101 Views

We have an array of objects like this −let data = [    {id:1, Name: "Abe", RowNumber: 1 },    {id:2, Name: "Bob", RowNumber: 2 },    {id:3, Name: "Clair", RowNumber: 3 },    {id:4, Name: "Don", RowNumber: 3.0 },    {id:5, Name: "Edna", RowNumber: 3.1 },    {id:6, Name: "Frank", RowNumber: 3.2 },    {id:7, Name: "Gabe", RowNumber: 4 },    {id:8, Name: "Helen", RowNumber: 5 },    {id:9, Name: "Isabelle", RowNumber: 6 },    {id:10, Name: "Jane", RowNumber: 7 },    {id:11, Name: "Ken", RowNumber: 8 }, ];We are required to write a JavaScript function that takes in ... Read More

Search and update array based on key JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:41:43

151 Views

We have two arrays like these −let arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"}] let arr2 = [{"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]We are required to write a function that adds the property level to each object of arr2, picking it up from the object from arr1 that have the same value for property "POSITION"Let's write the code for this function −Examplelet arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSI TION":"GMH"}]    let arr2 = [{"EMAIL":"test1@stc.com", "POSITION":"GM"},    {"EMAIL":"test2@stc.com", "POSITION":"GMH"},    {"EMAIL":"test3@stc.com", "POSITION":"RGM"},    {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ] const formatArray = (first, second) => {    second.forEach((el, ... Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
Updated on 31-Aug-2020 13:39:47

249 Views

We have an array of literals like this −const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Let's write the code for this function −Exampleconst arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; const splitElement = (arr, num) => {    return arr.map(el => {       if(String(el).length

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
Updated on 31-Aug-2020 13:37:24

222 Views

We have an array of arrays like this −const arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ]We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays.Let's write the code for this functionExampleconst arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ] const commonArray = arr => {    return arr.reduce((acc, val, index) => {       return acc.filter(el => val.indexOf(el) !== -1);    }); }; console.log(commonArray(arr));OutputThe output in the console will be −['bar']

Reverse numbers in function without using reverse() method in JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:32:48

1K+ Views

We are required to write a JavaScript function that takes in a number and returns its reversed number with converting it to an array or string.Let's write the code for this function −Exampleconst num = 234567; const reverseNumber = (num, res = 0) => {    if(num){       return reverseNumber(Math.floor(num / 10), (res*10)+(num % 10));    };    return res; }; console.log(reverseNumber(num)); console.log(reverseNumber(53536)); console.log(reverseNumber(76576));OutputThe output in the console will be −765432 63535 67567

Advertisements