Found 9150 Articles for Object Oriented Programming

Remove '0','undefined' and empty values from an array in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:21:15

240 Views

To remove ‘0’. ‘undefined’ and empty values, you need to use the concept of splice(). Let’s say the following is our array −var allValues = [10, false, 100, 150 ,'', undefined, 450, null]Following is the complete code using for loop and splice() −Examplevar allValues = [10, false, 100, 150 ,'', undefined, 450, null] console.log("Actual Array="); console.log(allValues); for (var index = 0; index < allValues.length; index++) {    if (!allValues[index]) {       allValues.splice(index, 1);       index--;    } } console.log("After removing false, undefined, null or ''..etc="); console.log(allValues);To run the above program, you need to use the following ... Read More

How to Iterate Elements by ClassName in JavaScript?

AmitDiwan
Updated on 21-Oct-2024 18:03:56

11K+ Views

To iterate elements by className in JavaScript, we will be using getElementsByClassName() method. It is used for getting the collection of all the elements in the document with specified classname. In this article we are having three div elements each having same class name. Our task is to iterate elements by className in JavaScript. Approaches to Iterate Elements by ClassName Here is a list of approaches to iterate elements by className in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes. Iterate Elements by ClassName using for loop ... Read More

Display all the numbers from a range of start and end value in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:14:55

888 Views

Let’s say the following is our start value −var startValue=10;Let’s say the following is our end value −var endValue=20;Use the for loop to fetch numbers between the start and end value −Examplevar startValue=10; var endValue=20; var total=''; function printAllValues(startValue, endValue){    for(var start=startValue;start < endValue ;start++){       total=total+start+", ";    } } printAllValues(startValue, endValue) var allSequences = total.slice(0, -1); console.log(allSequences);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo87.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo87.js 10, 11, 12, 13, 14, 15, 16, 17, 18, 19Read More

How could I write a for loop that adds up all the numbers in the array to a variable in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:14:02

941 Views

At first, declare and initialize the total variable with 0 and then you need to iterate over all the array and extract all the values of array and add up with the updated total variable.Let’s say the following is our array with numbers −var listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200];Examplevar listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200]; var total = 0; for(let index = 0; index < listOfValues.length ; index++){    total=total+listOfValues[index]; } console.log("Total Values="+total);To run the above program, you need to use the following command −node fileName.js.Here, my file name ... Read More

Capitalize only the first letter after colon with regex and JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:13:12

1K+ Views

To capitalize only the first letter, use the concept of regular expression along with toUpperCase(). Since the toUpperCase() capitalize the entire string, we need to use Regex to only capitalize the first letter after colon.Let’s say the following is our string −var objectValues='fullName: john Smith';Here’s the complete code to capitalize only the first letter after colon −Examplevar objectValues='fullName: john Smith'; function capitalizeFirstAfterTheColon(value){    return value.replace(/([:\?]\s+)(.)/g, function(data) {       return data.toUpperCase();    }); } console.log(capitalizeFirstAfterTheColon(objectValues));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo85.js.OutputThis will produce the following output −PS ... Read More

Any way to solve this without concatenating these two arrays to get objects with higher value?

AmitDiwan
Updated on 07-Sep-2020 08:12:08

51 Views

To get objects with specific property, use the concept of reduce() on both arrays individually. You don’t need to concatenate. Let’s say the following are our objects with student name and student marksvar sectionAStudentDetails = [    {studentName: 'John', studentMarks: 78},    {studentName: 'David', studentMarks: 65},    {studentName: 'Bob', studentMarks: 98} ]; let sectionBStudentDetails = [    {studentName: 'John', studentMarks: 67},    {studentName: 'David', studentMarks: 89},    {studentName: 'Bob', studentMarks: 97} ];Following is the code to implement reduce() on both and fetch the object with higher value (marks) −Examplevar sectionAStudentDetails = [    {studentName: 'John', studentMarks: 78},    {studentName: 'David', ... Read More

Filter array of objects by a specific property in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:10:43

563 Views

Use the concept of map() along with ternary operator (?). Following are our array of objects −let firstCustomerDetails = [    {firstName: 'John', amount: 100},    {firstName: 'David', amount: 50},    {firstName: 'Bob', amount: 80} ];    let secondCustomerDetails = [    {firstName: 'John', amount: 400},    {firstName: 'David', amount: 70},    {firstName: 'Bob', amount: 40} ];Let’s say, we need to filter array of objects by amount property. The one with the greatest amount is considered.Examplelet firstCustomerDetails = [    {firstName: 'John', amount: 100},    {firstName: 'David', amount: 50},    {firstName: 'Bob', amount: 80} ]; let secondCustomerDetails = [   ... Read More

JavaScript creating an array from JSON data?

AmitDiwan
Updated on 07-Sep-2020 08:09:27

919 Views

To create an array from JSON data, use the concept of map() from JavaScript. Let’s say the following is our data −const studentDetails =[    {       name : "John"    },    {       name : "David"    },    {       name : "Bob"    } ];Following is the code to create an array from the above data −Exampleconst studentDetails =[    {       name : "John"    },    {       name : "David"    },    {       name : "Bob"    } ]; const ListOfStudentName = studentDetails.map(({name:actualValue})=>actualValue); console.log(ListOfStudentName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo82.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo82.js [ 'John', 'David', 'Bob']

Is there a “null coalescing” operator in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:08:05

198 Views

Yes, JavaScript now supports the “null coalescing” operator, but you can also use the concept of logical OR (||). The syntax is as follows −var anyVariableName=null; var anyVariableName=yourVariableName || yourActualValue;Examplevar fullName=null; console.log("The full name is="+fullName); var actualName=fullName || "David Miller"; console.log("The full name is="+actualName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo81.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo81.js The full name is=null The full name is=David Miller

JavaScript fetch a specific value with eval()?

Disha Verma
Updated on 13-Mar-2025 13:04:51

396 Views

The eval() function in JavaScript allows the execution of code stored as a string. It can be used to fetch specific values dynamically, but be careful, as it can have security risks and affect performance. This article will guide you on how to use eval() to get specific values and why you should generally avoid using eval(). How eval() Works? The eval() function takes a string as an argument and evaluates it as JavaScript code. For example: console.log(eval("2 + 2")); // Output: 4 Fetching Specific Values Using eval() The eval() function in JavaScript can be used to: ... Read More

Advertisements