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
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']
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
For this, use a “prototype”. JavaScript objects inherit properties and methods from a prototype. For accessing variables, we have also used the “this” in JavaScript.Examplefunction Customer(fullName){ this.fullName=fullName; } Customer.prototype.setFullName = function(newFullName){ this.fullName=newFullName; } var customer=new Customer("John Smith"); console.log("Using Simple Method = "+ customer.fullName); customer.setFullName("David Miller"); console.log("Using Prototype Method = "+customer.fullName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo79.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo79.js Using Simple Method = John Smith Using Prototype Method = David MillerRead More
Let’s say the following is our Unordered List (ul) − JavaScript Remove MySQL Remove MongoDB Remove Java Remove Above, you can see the “Remove” button with every li element. On clicking this button, you can remove any li element.Following is the code to remove li elements on button click;Example Live Demo Document Remove the subjects The Subjects are as follows: JavaScript Remove MySQL Remove MongoDB Remove Java Remove var allSubjectName = document.querySelectorAll(".subjectName"); for (var index = 0; index
You can use the concept of Map(). In JavaScript, map has the key value concept in which key must be unique.We also need to use the join() method to join Map values. Following is the code −Examplelet queryStringAppendWithURL = new Map(); queryStringAppendWithURL.set("firstParamter", ["name=John", "age=23", "countryName=US"]); queryStringAppendWithURL.set("secondParamter", ["subjectName=JavaScript", "Marks=91"]); let appendValue = Array.from(queryStringAppendWithURL.values()).map(value => value.join('?')).join('?'); console.log("The appended value is="+appendValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo78.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo78.js The appended value is=name=John?age=23?countryName=US?subjectName=JavaScript?Marks=91Read More
To conditionally change object property, use the logical AND operator ( &&).If both the operands are non-zero, then the condition becomes true in JavaScript’s logical AND operator.Examplelet marksDetails = { Mark1: 33, Mark2: 89 }, isBacklog = false; console.log("Result when backlog is set to false==="); console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 33 }}); isBacklog = true; console.log("Result when backlog is set to true==="); console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 93 }});To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo77.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More
To remove leading zeroes in an array, use the concept of filter(). Following is our input wherein we will remove the leading zeroes −[10,0,12,0,0]) [0,0,0,0,0,0,10,12,0]) [12,0,0,1,0,0])Exampleconst removeLeadingZero = input => input.filter((lastValue => value => lastValue= lastValue || value) (false) ); console.log(removeLeadingZero([10,0,12,0,0])); console.log(removeLeadingZero([0,0,0,0,0,0,10,12,0])); console.log(removeLeadingZero([12,0,0,1,0,0]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo76.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo76.js [ 10, 0, 12, 0, 0 ] [ 10, 12, 0 ] [ 12, 0, 0, 1, 0, 0 ]
To change value of input, use the concept of document. The syntax is as follows −document.yourFormName.yourTextName.value=anyValue;Let’s say the following is our input type calling submitForm() on clicking “Submit Form” − The submitForm() function changing the value of input −function submitForm(){ document.studentForm.txtInput.value='STUDENT-100'; }Example Live Demo Document function submitForm(){ document.studentForm.txtInput.value='STUDENT-100'; } 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 ... Read More
If onclick not working for two or more links, then use the document.querySelectorAll() in JavaScript. Following is the code −Example Live Demo Document www.example.com www.test.com const values = document.querySelectorAll('[class=class1]'); values.forEach(function(v) { console.log(v.innerHTML); }); 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 −