Found 10483 Articles for Web Development

Replace array value from a specific position in JavaScript

AmitDiwan
Updated on 12-Sep-2020 07:26:43

536 Views

To replace value from a specific position, use splice() in JavaScript. Following is the code −Examplevar changePosition = 2 var listOfNames = ['John', 'David', 'Mike', 'Sam','Carol'] console.log("Before replacing="); console.log(listOfNames); var name = 'Adam' var result = listOfNames.splice(changePosition, 1, name) console.log("After replacing="); console.log(listOfNames)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo14.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo154.js Before replacing= [ 'John', 'David', 'Mike', 'Sam', 'Carol' ] After replacing= [ 'John', 'David', 'Adam', 'Sam', 'Carol' ]

JavaScript Recursion finding the smallest number?

AmitDiwan
Updated on 11-Sep-2020 08:41:17

251 Views

Let’s say the following is our array −var numbers=[10,101,76,56,5,210,3,100];To find the smallest number, the code is as follows −Examplefunction findMinimumElementUsingRecursive(numbers) {    if (numbers.length==1){       return numbers[0];    }    else if(numbers[0]>numbers[1]) {       return findMinimumElementUsingRecursive(numbers.slice(1));    } else {       return       findMinimumElementUsingRecursive([numbers[0]].concat(numbers.slice(2)));    } } var numbers=[10,101,76,56,5,210,3,100]; console.log("The minimum element is="+findMinimumElementUsingRecursive(numbers));To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo152.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo152.js The minimum element is=3

JavaScript filter array by multiple strings

AmitDiwan
Updated on 29-Nov-2024 19:11:57

1K+ Views

Filtering an array by multiple strings in JavaScript involves identifying elements that match any string from a given list. This is commonly used in search filters, dynamic matching, or data processing tasks. JavaScript provides simple tools like the filter method and techniques such as includes for exact matches or regular expressions for pattern-based matching. These approaches help efficiently narrow down arrays based on specific criteria. Approaches to filter array by multiple strings Here are the following approaches to filter an array by multiple strings in JavaScript using regex (case-sensitive) and filter with includes: Using Regular Expressions ... Read More

Split a URL in JavaScript after every forward slash?

AmitDiwan
Updated on 11-Sep-2020 08:36:01

9K+ Views

To split a URL, use the split() method. Use toString() before that. Let us see an exampleExamplevar newURL="http://www.example.com/index.html/homePage/aboutus/"; console.log(newURL); var splitURL=newURL.toString().split("/"); console.log(splitURL);Above, we have set forward slash in the split() function, since we need to split the URL after every such slash.To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo150.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo150.js http://www.example.com/index.html/homePage/aboutus/[    'http:',    '',    'www.example.com',    'index.html',    'homePage',    'aboutus',    '' ]Read More

Can someone explain to me what the plus sign is before the variables in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:28:48

3K+ Views

The plus(+) sign before the variables defines that the variable you are going to use is a number variable.In the below code, there is brief description about plus sign. Following is the code −Examplevar firstValue="1000"; console.log("The data type of firstValue ="+typeof firstValues); var secondValue=1000; console.log("The data type of secondValue ="+typeof secondValue); console.log("The data type of firstValue when + sign is used ="+typeof +firstValue); var output=+firstValue + secondValue; console.log("Addition is="+output);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo149.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo149.js The data type ... Read More

Display a message on console while focusing on input type in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:27:14

254 Views

For this, you can use the concept of focus(). Following is the code −Example Live Demo Document Submit    const submitButton = document.getElementById('submitBtn');    submitButton.addEventListener('click', () => {       console.log('Submitting information to the server.....');    })    const txtInput = document.getElementById('txtInput');    txtInput.addEventListener('blur', () => {       console.log('Enter the new value into the text box......');       txtInput.focus();    });    txtInput.focus(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open ... Read More

Filter null from an array in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:23:15

1K+ Views

To filter null from an array and display only non-null values instead, use filter(). Following is the code −Examplevar names=[null, "John", null, "David", "", "Mike", null, undefined, "Bob", "Adam", null, null]; console.log("Before filter null="); console.log(names); var filterNull=[]; filterNullValues=names.filter(obj=>obj); console.log("After filtering the null values="); console.log(filterNullValues);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo148.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo148.js Before filter null=[    null, 'John',    null, 'David',    '', 'Mike',    null, undefined,    'Bob', 'Adam',    null, null ] After filtering the null values= [ ... Read More

Retrieving object's entries in order with JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:21:14

254 Views

Let’s say the following is our object −const subjectDetails ={    102:"Java",    105:"JavaScript",    104:"MongoDB",    101:"MySQL" };Use sort() method to retrieve object’s entry in order −const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => {    return [k, subjectDetails[k]]; }));Exampleconst subjectDetails ={    102:"Java",    105:"JavaScript",    104:"MongoDB",    101:"MySQL" }; const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => {    return [k, subjectDetails[k]]; })); console.log(orderSubjects);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo146.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo146.js{    '101': 'MySQL',    '102': 'Java',    '104': 'MongoDB',    '105': ... Read More

Print JSON nested object in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:20:11

1K+ Views

To print JSON nested object in JavaScript, use for loop along with JSON.parse(). Following is the code −Examplevar details = [    {       "studentId": 101,       "studentName": "John",       "countryName": "US",       "subjectDetails": "{\"0\":\"JavaScript\",\"1\":\"David\"}"    },    {       "studentId": 102,       "studentName": "Bob",       "countryName": "UK",       "subjectDetails": "{\"0\":\"Java\",\"1\":\"Carol\"}"    },    {       "studentId": 103,       "studentName": "Mike",       "countryName": "AUS",       "subjectDetails": "{\"0\":\"MongoDB\",\"1\":\"Adam\"}"    } ] for (const detailsObject of details) {    const subjectDetailsObject =    JSON.parse(detailsObject.subjectDetails);    console.log(subjectDetailsObject[0]); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo145.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo145.js JavaScript Java MongoDB

Getting HTML form values and display on console in JavaScript?

AmitDiwan
Updated on 11-Sep-2020 08:18:51

6K+ Views

To get HTML form values, use the value property. Let’s say the following is our input type −We need to display the above value “My name is John Smith” on console.Example Live Demo Document    var originalvalue = document.getElementById("getValues").value;    console.log("The value in the text box is ="+originalvalue); 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 −

Advertisements