AmitDiwan has Published 10744 Articles

How to create a custom function similar to find() method in JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2020 12:56:13

423 Views

Let’s say we have the following records of studentId and studentName and want to check a specific student name −const studentDetails= [    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"David"    },    {     ... Read More

Demonstrate nested loops with return statements in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:58:48

625 Views

Here’s an example with two loops, outer and inner −Examplelet demoForLoop = ()=>{    for(var outer=1;outer

Converting any string into camel case with JavaScript removing whitespace as well

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:52:41

1K+ Views

In order to convert string into camel case, you need to lowercase the first letter of the word and the first letter of the remaining words must be in capital.Following is the code to convert any string into camel case −Examplefunction convertStringToCamelCase(sentence) {    return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g,    function(camelCaseMatch, i) { ... Read More

How to display only the current year in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:51:37

450 Views

To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year=    document.getElementById("theCurrentYear").innerHTML = new    Date().getFullYear(); To run the above program, save the file name “anyName.html(index.html)” and right ... Read More

Strip quotes with JavaScript to convert into JSON object?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:50:06

2K+ Views

For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use ... Read More

Copy objects with Object.assign() in JavaScript

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:48:54

168 Views

The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = ... Read More

Is there a DOM function which deletes all elements between two elements in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:47:19

307 Views

Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the ... Read More

Replace HTML div into text element with JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:45:24

823 Views

For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Live Demo Document My Name is John My h6 value must be here... My h6 value must be here... My h6 ... Read More

Update array of objects with JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:42:02

433 Views

Let’s say the following are our array of objects −var studentDetails = [    { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']},    {firstName: "David", listOfSubject: ['Java', 'C'] }]We need to add the following in the already created array of objects −{firstName: "Bob", listOfSubject: ['JavaScript']};Examplevar studentDetails = [    { firstName: "John", ... Read More

How to add properties from one object into another without overwriting in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:40:22

644 Views

Let’s say the following are our objects −var first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000}You can use the concept of hasOwnProperty() to add properties from one object to another. Following is the code −Examplevar first = {key1: 100, key2: 40, ... Read More

Advertisements