Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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?
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 MoreJavaScript recursive loop to sum all integers from nested array?
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 MoreCreate empty array of a given size in JavaScript
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 MoreBetter ways to modify string with multiple methods using JavaScript
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 MoreTransform nested array into normal array with JavaScript?
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 MoreHow to get only the first BOT ID from thes JavaScript array?
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 MoreHow can I instantiate a dictionary in JavaScript where all keys map to the same value?
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 MoreLooping in JavaScript to count non-null and non-empty values
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 MoreHow to remove an object using filter() in JavaScript?
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 MoreHow to transform object of objects to object of array of objects with JavaScript?
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