Object Oriented Programming Articles

Page 382 of 588

How to generate array of n equidistant points along a line segment of length x with JavaScript?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 385 Views

To generate array of n equidistant points along a line segment of length x, use the below syntax −for (var anyVariableName= 0; yourVariableName< yourStartPointName; yourVariableName++) {    var v = (yourVariableName+1)/ (yourStartPointName+1);    var v2 = v*yourEndPointName;Examplefunction drawPoints(start, end) {    const arrayOfPoints = []    for (var index = 0; index < start; index++) {       var v = (index+1)/ (start+1);       var v2 = v*end;       arrayOfPoints.push(Math.round(v2));    }    return arrayOfPoints; } const arrayOfPoints = drawPoints(5, 50); console.log("The Points="); console.log(arrayOfPoints);To run the above program, you need to use the following command ...

Read More

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 456 Views

You need to call the same function again and again to sum all integers from nested array. Following is the code −Examplefunction sumOfTotalArray(numberArray){    var total= 0;    for (var index = 0; index < numberArray.length; index++) {       if (numberArray[index] instanceof Array){          total=total+sumOfTotalArray(arr[index]);       }       if (numberArray[index] === Math.round(numberArray[index])){          total=total+numberArray[index];       }    }    return total; } var number = new Array(6); number=[10, 20, 30, 40, 50, 60]; console.log("The sum is="+sumOfTotalArray(number));To run the above program, you need to use the following ...

Read More

Create empty array of a given size in JavaScript

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 2K+ Views

To create an empty array of given size, use the new operator −var numberArray = new Array(10);After that, let’s set some values in the array. Following is the code −Examplevar numberArray = new Array(10); console.log("The length="+numberArray.length) numberArray=[10,20,30,40,50,60]; console.log("The array value="); for(var i=0;i node demo52.js The length=10 The array value= 10 20 30 40 50 60

Read More

Better ways to modify string with multiple methods using JavaScript

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 415 Views

To modify string, you can use toLowerCase() as well as toUpperCase(). Let’s say the following is our string −var sentence = "tHIS iS tHE JavaScript pROGRAM";To modify and display in proper case, the code is as follows −Examplevar sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) {    return sentence.charAt(0).toUpperCase() +    sentence.slice(1).toLowerCase(); } console.log(modifyStringWithMultipleMethods(sentence));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo51.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo51.js This is the JavaScript program

Read More

Transform nested array into normal array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 321 Views

Let’s say the following is our nested array −const arrayObject = [    [       {          Name: "John"       },       {          countryName: "US"       }    ],    [       {          subjectName: "JavaScript"       },       {          teacherName: "Mike"       }    ] ];To transform nested array into normal array, use the concept of flat() as in the below code −Exampleconst arrayObject = [    [       {          Name: "John"       },       {          countryName: "US"       }    ],    [       {          subjectName: "JavaScript"       },       {          teacherName: "Mike"       }    ] ]; const output = arrayObject.flat(); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo50.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo50.js [    { Name: 'John' },    { countryName: 'US' },    { subjectName: 'JavaScript' },    { teacherName: 'Mike' } ]

Read More

How to get only the first BOT ID from thes JavaScript array?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 129 Views

Let’s say we have records with BOTID and Name of assigned users −let objectArray = [    { BOTID: "56", Name: "John" },    { BOTID: "57", Name: "David" },    { BOTID: "58", Name: "Sam"},    { BOTID: "59", Name: "Mike" },    { BOTID: "60", Name: "Bob" } ];We know the array starts from index 0. If you want to access the first element from the above array, use the below syntax −var anyVariableName=yourArrayObjectName[index].yourFieldName;Examplelet objectArray = [    { BOTID: "56", Name: "John" },    { BOTID: "57", Name: "David" },    { BOTID: "58", Name: "Sam"},   ...

Read More

How can I instantiate a dictionary in JavaScript where all keys map to the same value?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 306 Views

At first, set the keys −const name = ['Name1', 'Name2'];Now, map keys to the same value using for loop as in the below code −Exampleconst name = ['Name1', 'Name2']; const keyValueObject = {}; for (const k of name){    keyValueObject[k] = 'John'; } console.log(keyValueObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo48.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo48.js { Name1: 'John', Name2: 'John' }

Read More

Looping in JavaScript to count non-null and non-empty values

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 1K+ Views

Let’s say the following are our values −let subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java'];To count the non-empty and non-null values, use the forEach(). The syntax is as follows −yourArrayName.forEach(anyVariableName =>{    yourStatement1    .    .    .    N    } } )Now, use the if statement and check −var count=0 subjectNames.forEach(subject =>{    if(subject!=' ' || subject!=null){       count+=1;       }    } )Examplelet subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java']; var count=0 subjectNames.forEach(subject =>{    if(subject!=' ' || subject!=null){          count+=1;       }    } ) console.log("Number of subject=="+count);To ...

Read More

How to remove an object using filter() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 03-Sep-2020 640 Views

Let’s say, we have the following objects in JavaScript −ObjectValue =[    { "id": "101", "details": { Name:"John", subjectName:"JavaScript" }},    { "id": "102", "details": { Name:"David", subjectName:"MongoDB" }},    { "id": "103" } ]To remove an object using filter(), the code is as follows −ExampleObjectValue =[    { "id": "101", "details": { Name:"John", subjectName:"JavaScript" }},    { "id": "102", "details": { Name:"David", subjectName:"MongoDB" }},    { "id": "103" } ] output=ObjectValue.filter(obj=>obj.details) console.log(output)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo46.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo46.js ...

Read More

How to transform object of objects to object of array of objects with JavaScript?

AmitDiwan
AmitDiwan
Updated on 02-Sep-2020 824 Views

To transform object of objects to object of array of objects, use the concept of Object.fromEntries() along with map().Exampleconst studentDetails = {    'details1': {Name: "John", CountryName: "US"},    'details2': {Name: "David", CountryName: "AUS"},    'details3': {Name: "Bob", CountryName: "UK"}, }; console.log(    Object.fromEntries(Object.entries(studentDetails).map(([key,    value]) => [key, [value]])) );To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo45.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo45.js {    details1: [ { Name: 'John', CountryName: 'US' } ],    details2: [ { Name: 'David', CountryName: 'AUS' } ],    details3: ...

Read More
Showing 3811–3820 of 5,877 articles
« Prev 1 380 381 382 383 384 588 Next »
Advertisements