Found 9150 Articles for Object Oriented Programming

Assign new value to item in an array if it matches another item without looping in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:39:21

118 Views

For this, use filter() along with map(). Let’s say the following is our array −const studentDetails = [    {Name: "John"},    {Name: "David"},    {Name: "Bob"},    {Name: "Mike"} ]We will assign a new value to the name “Bob”. Following is the code −Exampleconst studentDetails = [    {Name: "John"},    {Name: "David"},    {Name: "Bob"},    {Name: "Mike"} ] var changeName = "Bob"; studentDetails.filter((obj) => obj.Name === changeName).map((obj) => obj.Name = "Carol"); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo98.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More

How to disallow altering of object variables in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:38:31

144 Views

Use the concept of freeze() from JavaScript to disallow adding new properties to an object, altering of object properties, etc.Following is the code wherein we are changing the value but the previous value still remains since we cannot alter properties using freeze() −Exampleconst canNotChangeTheFieldValueAfterFreeze = {value1 : 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); canNotChangeTheFieldValueAfterFreeze.value = 100; console.log("After changing the field value1 from 10 to 100 ="+canNotChangeTheFieldValueAfterFreeze.value1);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo97.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo97.js After changing the field value1 from 10 ... Read More

Extract key value from a nested object in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:36:56

3K+ Views

Let us first create a nested object −var details = {    "teacherDetails":    {       "teacherName": ["John", "David"]    },    "subjectDetails":    {       "subjectName": ["MongoDB", "Java"]    } }Let us now extract the keys. Following is the code −Examplevar details = {    "teacherDetails":    {       "teacherName": ["John", "David"]    },    "subjectDetails":    {       "subjectName": ["MongoDB", "Java"]    } } var objectName, nestedObject; var name = "Java"; for(var key in details){    for(var secondKey in details[key]){       if(details[key][secondKey].includes(name)){          objectName = ... Read More

Sleep in JavaScript delay between actions?

AmitDiwan
Updated on 07-Sep-2020 08:34:11

465 Views

To set sleep i.e. delay, use the concept of setTimeout(). It takes values in milliseconds i.e.1000 milliseconds = 1 second 2000 milliseconds = 2 seconds, etc.For our example, we will add two values with a delay of 5 seconds i.e. 5000 milliseconds. Following is the code −Examplevar firstValue=10; var secondValue=20; var result=firstValue+secondValue; setTimeout(function() { }, (5 * 1000)); console.log("The result="+result);The above setTimeout() will sleep for 5 seconds. To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo95.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo95.js The result=30Read More

Return Largest Numbers in Arrays passed using reduce method?

AmitDiwan
Updated on 07-Sep-2020 08:32:50

167 Views

To get largest numbers passed using reduce(), use the Math.max() function. Following is the code −const getBiggestNumberFromArraysPassed = allArrays => allArrays.reduce( (maxValue, maxCurrent) => maxValue.push(Math.max(...maxCurrent)),maxValue),[]); console.log(getBiggestNumberFromArraysPassed([[45,78,3,1],[50,34,90,89],[32,10,90,99]]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo94.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo94.js [ 78, 90, 99 ]

Remove characters from a string contained in another string with JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:30:03

362 Views

Let’s say the following are our two strings −var originalName = "JOHNDOE"; var removalName = "JOHN"We need to remove the second string from first. For this, use replace() along with reduce().Exampleconst removeCharactersFromAString= (removalName,originalName)=>removalName.split('').reduce((obj,v)=>obj.replace(v,''),originalName); var originalName = "JOHNDOE"; var removalName = "JOHN" console.log(removeCharactersFromAString(removalName,originalName));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo93.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo93.js DOE

Un-nesting array of object in JavaScript?

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

288 Views

To un-nest array of objects, use the concept of map(). Let’s say the following are our array of objects −const studentDetails = [    {       "studentId": 101,       "studentName": "John",       "subjectDetails": {          "subjectName": "JavaScript"       }    },    {       "studentId": 102,       "studentName": "David",       "subjectDetails": {          "subjectName": "MongoDB"       }    } ];We need to un-nest the subjectName and display the result. Following is the code −Exampleconst studentDetails = [   ... Read More

Multiple Variables Holding Data - Which has the Highest Value in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:27:09

439 Views

To fetch the highest value, use the Math.max() from JavaScript. Since, we want the one with maximum value, use the Object.values.Exampleconst studentMarksDetails= {    marks1:78,    marks2:69,    marks3:79,    marks4:74 } const maximumMarks = Math.max(...Object.values(studentMarksDetails)); console.log("The highest marks="+maximumMarks); for (const key of Object.keys(studentMarksDetails)){    if (studentMarksDetails[key] == maximumMarks) {       console.log(`The Object='${key}'`);       break;    } }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo91.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo91.js The highest marks=79 The Object='marks3'Read More

Remove extra spaces in string JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:23:52

766 Views

To remove extra spaces, you need to use trim() along with regular expressions. Following is our string with spaces before applying trim() −var sentence="My name is John Smith ";Now, we will remove the extra spaces as in the below code −Examplevar sentence="My name is John Smith "; console.log("The actual value="); console.log(sentence); var originalSentence = sentence.replace(/\s+/g, '').trim(); var afterRemovingTheSpace=originalSentence.trim(); console.log("After removing the spaces="); console.log(afterRemovingTheSpace);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo90.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo90.js The actual value= My name is John Smith After removing ... Read More

How to modify key values in an object with JavaScript and remove the underscore?

AmitDiwan
Updated on 07-Sep-2020 08:22:35

570 Views

In order to modify the key values, you need to use regular expressions along with Object.fromEntries().Examplevar underscoreSpecifyFormat = str => str.replace(/(_)(.)/g, (_, __, v) => v.toUpperCase()), JsonObject = { first_Name_Field: 'John', last_Name_Field: 'Smith'}, output = Object.fromEntries(Object.entries(JsonObject).map(([key, value]) => [underscoreSpecifyFormat(key), value])); console.log("The JSON Object="); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo89.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo89.js The JSON Object= { firstNameField: 'John', lastNameField: 'Smith' }

Advertisements