AmitDiwan has Published 10744 Articles

How to decrease size of a string by using preceding numbers - JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 09:05:13

104 Views

Let’s say our original string is the following with repeated letters −var values = "DDAAVIDMMMILLERRRRR";We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression.ExampleFollowing is the code −var values = "DDAAVIDMMMILLERRRRR"; var precedingNumbersInString = values.replace(/(.)\1+/g, obj => obj.length + obj[0]); ... Read More

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 09:03:13

170 Views

For this, use Object.keys() and set one key on each iteration as null using a for loop..ExampleFollowing is the code −var objectValues = {    "name1": "John",    "name2": "David",    "address1": "US",    "address2": "UK" } for (var tempKey of Object.keys(objectValues)) {    var inEachIterationSetOneFieldValueWithNull = {     ... Read More

Set the value in local storage and fetch – JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 09:00:59

593 Views

Use localStorage.setItem(“anyKeyName”, yourValue) to set the value in local storage and if you want to fetch value then you can use localStorage.getItem(“yourKeyName”)ExampleFollowing is the code − Live Demo            Document        var textBoxNameHTML = document.getElementById('txtName'); ... Read More

How to sort array by first item in subarray - JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:57:52

526 Views

Let’s say we have the following array −var studentDetails = [    [89, "John"],    [78, "John"],    [94, "John"],    [47, "John"],    [33, "John"] ];And we need to sort the array on the basis of the first item i.e. 89, 78, 94, etc. For this, use sort().ExampleFollowing is ... Read More

How to remove duplicate property values in array – JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:55:46

802 Views

Let’s say the following is our array −var details = [    {       studentName: "John",       studentMarks: [78, 98]    },    {       studentName: "David",       studentMarks: [87, 87]    },    {       studentName: "Bob",     ... Read More

Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:51:33

291 Views

For this, use the addEventListener() with mousedown event.ExampleFollowing is the code −            Document    document.addEventListener("mousedown", function () {       console.log("Mouse down event is happening");    }); To run the above program, save ... Read More

Add time to string data/time - JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:49:22

1K+ Views

At first, set a new Date in JavaScript −var dateValue = new Date("2021-01-12 10:10:20");Use new Date() along with setHours() and getHours() to add time.ExampleFollowing is the code −var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); console.log("The date value is=" + dateValue.toString()); console.log("Only Hours value after incrementing=" + dateValue.getHours());To run ... Read More

How to generate array key using array index – JavaScript associative array?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:47:57

342 Views

For this, use forEach() along with [] to an associative array.ExampleFollowing is the code −var result = {}; var names = ['John', 'David', 'Mike', 'Sam', 'Bob', 'Adam']; names.forEach((nameObject, counter) => {    var generatedValues = { [nameObject]: counter };    Object.assign(result, generatedValues) }) console.log(result);To run the above program, you need ... Read More

How to change the color of a button when the input field is filled – JavaScript?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:34:56

2K+ Views

Let’s say the following is our button −Press MeOn filling the below input field, the color of the above button should change −ExampleFollowing is the code − Live Demo            Document           UserName:     ... Read More

In JavaScript, what is meant by 'a function expression is always a constant value'?

AmitDiwan

AmitDiwan

Updated on 09-Nov-2020 08:27:33

132 Views

If the const is used in a program, and if you try to reassign the value to const variable then an error will arise.Let’s say the following is our const variable −const result = (first, second) => first * second;Now, we will try to reassign a value to the const ... Read More

Advertisements