Found 10483 Articles for Web Development

Select random values from an array in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:56:31

538 Views

To select random values from an array, use the concept of Math.random().Examplevar subjectNames = ["Javascript", "MySQL", "Java", "MongoDB", "Python","Spring Framework"]; for(var index = subjectNames.length - 1; index > 0; index--){    var rndIndex = Math.floor(Math.random() * (index + 1));    var subjNameTemp = subjectNames[rndIndex];    subjectNames[rndIndex] = subjectNames[index];    subjectNames[index] = subjNameTemp; } var getRandomSubjectName = subjectNames.slice(0, 3); console.log(getRandomSubjectName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo178.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo178.js [ 'Javascript', 'MySQL', 'Python' ]

Get only specific values in an array of objects in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:55:15

13K+ Views

Let’s say the following is our array of objects −var details = [{    studentName: "John",    studentMarks: 92 }, {    studentName: "David",    studentMarks: 89 }, {    studentName: "Mike",    studentMarks: 98 }, ];To get only specific values in an array of objects in JavaScript, use the concept of filter().Examplevar details = [{    studentName: "John",    studentMarks: 92 }, {    studentName: "David",    studentMarks: 89 }, {    studentName: "Mike",    studentMarks: 98 }, ]; var specificValuesFromArray = details.filter(obj => obj.studentMarks === 92 || obj.studentMarks === 98); console.log(specificValuesFromArray)To run the above program, you need to ... Read More

How to add a new object into a JavaScript array after map and check condition?

AmitDiwan
Updated on 12-Sep-2020 08:54:09

415 Views

For this, you can use filter() along with map().Exampleconst details =[    { customerName: 'John', customerCountryName: 'UK', isMarried :true },    { customerName: 'David', customerCountryName: 'AUS', isMarried :false },    { customerName: 'Mike', customerCountryName: 'US', isMarried :false } ] let tempObject = details.filter(obj=> obj.isMarried == true); tempObject["customerNameWithIsMarriedFalse"] = details.filter(obj => obj.isMarried== false).map(obj => obj.customerName); console.log(tempObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo176.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo176.js [    { customerName: 'John', customerCountryName: 'UK', isMarried: true }, customerNameWithIsMarriedFalse: [ 'David', 'Mike' ] ]

How to create an increment of 10 value once you click a button in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:52:28

1K+ Views

For this, use click() along with parseInt().Example Live Demo Document 10 addValue10EachTimePressMe    addValue = 0;    $("#addSequenceOf10").click(function() {       var actualValue = parseInt($("#add").html());       addValue =addValue+ actualValue;       $("#sequenceValue").html(addValue);    }); To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce the following output −Now, press the button you will get 10 then 20 30 40…….N; as in the below output −After clicking one more time, the snapshot is as follows.This will produce the following output −

JavaScript Regex to remove text after a comma and the following word?

Alshifa Hasnain
Updated on 23-Dec-2024 11:18:14

752 Views

When working with strings in JavaScript, you might encounter scenarios where you need to clean or format text by removing specific portions. A common task is to remove text after a comma and the following word. This can be achieved efficiently using JavaScript Regular Expressions (Regex). In this article, we’ll show you how to do it step-by-step. Why Use Regex for Text Manipulation? Regex, short for Regular Expressions, is a powerful tool for pattern matching and text processing. It allows you to identify and manipulate text patterns with precision, making tasks like cleaning data or reformatting strings much simpler. How ... Read More

Iterating and printing a JSON with no initial key and multiple entries?

AmitDiwan
Updated on 12-Sep-2020 08:43:56

60 Views

For iterating and printing, use forEach() loop in JavaScript.Exampleconst details =[    {       "studentId":101,       "studentName": "John Doe",    },    {       "studentId":102,       "studentName": "David Miller",    }, ]; details.forEach(obj=>{    console.log("StudentId="+obj.studentId);    console.log("StudentName="+obj.studentName); })To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo174.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo174.js StudentId=101 StudentName=John Doe StudentId=102 StudentName=David Miller

Is having the first JavaScript parameter with default value possible?

AmitDiwan
Updated on 12-Sep-2020 08:36:22

101 Views

You can use destructed array in this case.Examplefunction multiply(firstParameterDefaultValue=10, secondParameterValue) {    return firstParameterDefaultValue * secondParameterValue; } console.log("The result="+multiply(...[,10]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo173.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo173.js The result=100

Set a default value for the argument to cover undefined errors while calling a function in JavaScript

AmitDiwan
Updated on 12-Sep-2020 08:32:21

185 Views

If you won’t pass value to a function(), it will print the default value otherwise given parameter will be printed.Following is the code. We are setting a default here i.e. “Jack” in this case to avoid any undefined error when a function is called without any parameter −Examplefunction display({ name = 'Jack' } = {}) {    console.log(`Hi My Name is ${name}!`); } display(); display({name:"Taylor Swift"});To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo171.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo171.js Hi My Name is Jack! Hi My ... Read More

In JavaScript, can be use a new line in console.log?

AmitDiwan
Updated on 12-Sep-2020 08:29:15

2K+ Views

Yes, we can use a new line using “” in console.log(). Following is the code −Exampleconst studentDetailsObject = new Object() studentDetailsObject.name = 'David' studentDetailsObject.subjectName = 'JavaScript' studentDetailsObject.countryName = 'US' studentDetailsObject.print = function(){    console.log('hello David'); } console.log("studentObject", "", studentDetailsObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo170.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo170.js studentObject {    name: 'David',    subjectName: 'JavaScript',    countryName: 'US',    print: [Function] }

Prettify JSON data in textarea input in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:25:59

10K+ Views

For this, use JSON.parse() along with JSON.stringify().Example Live Demo Document Click The Button To get the Pretty JSON    function printTheJSONInPrettyFormat() {       var badJSON = document.getElementById('prettyJSONFormat').value;       var parseJSON = JSON.parse(badJSON);       var JSONInPrettyFormat = JSON.stringify(parseJSON, undefined, 4);       document.getElementById('prettyJSONFormat').value =       JSONInPrettyFormat;    } To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce ... Read More

Advertisements