Found 9150 Articles for Object Oriented Programming

Remove elements from array in JavaScript using includes() and splice()?

AmitDiwan
Updated on 03-Sep-2020 07:43:20

359 Views

The includes() check whether array has a specific element, whereas splice() is used to add/remove items. Following is the code −ExampledeleteElementsFromArray = function(elements, ...values) {    let elementRemoved = Array.from(values);    for (var index = 0; index < elements.length; index++){       if (elementRemoved.includes(elements[index])){          elements.splice(index, 1);          index--;       }    }    return elements; } console.log(deleteElementsFromArray([80,90,56,34,79], 90, 34,79));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo69.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo69.js [ 80, 56 ]

How to print all students name having percentage more than 70% in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:40:32

545 Views

You can use a for loop and check whether the percentage is greater than 70 or not with if condition.Following are the records of each student −const studentDetails= [    {       studentName:"John",       percentage:78    },    {       studentName:"Sam",       percentage:68    },    {       studentName:"Mike",       percentage:88    },    {       studentName:"Bob",       percentage:70    } ]Now, use the for loop and et conditions for students with percentage more than 70 −for(var index=0;index 70)       {   ... Read More

How can I get seconds since epoch in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:38:17

390 Views

To get seconds since epoch, use the below syntax −var anyVariableName= new Date(); Math.round(yourDateVariableName.getTime() / 1000);At first, get the current date −var currentDate= new Date();Now, get seconds since epoch −Examplevar currentDate= new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log(epochSeconds);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo67.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo67.js 1594821507

How to determine if date is weekend in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:33:11

790 Views

As we know the value 0 is for day Sunday and 6 for Saturday. First of all, you need to get day with the help of getDay().Let’s set a date −var givenDate=new Date("2020-07-18");Now, we will get the day −var currentDay = givenDate.getDay();Following is the code to determine if date is weekend −Examplevar givenDate=new Date("2020-07-18"); var currentDay = givenDate.getDay(); var dateIsInWeekend = (currentDay === 6) || (currentDay === 0); if(dateIsInWeekend==true){    console.log("The given date "+givenDate+" is a Weekend"); } else {    console.log("The given date " +givenDate+"is a not a Weekend"); }To run the above program, you need to use the ... Read More

Filter array with filter() and includes() in JavaScript

Revathi Satya Kondra
Updated on 28-Jan-2025 11:48:32

14K+ Views

In this article, using the filter() and includes() methods in JavaScript, we can filter an array based on the elements presence in another array or specific conditions. For suppose we have two arrays as array1 and array2. we want to filter array1 to include only the elements that are present in array2. To have the clear idea about the filter() and includes() in JavaScript. Let's discuss individually. JavaScript filter() and includes() Methods filter(): It is used to create a new array that includes only the elements that satisfy the condition provided by the callback function. includes(): Using includes(), the callback ... Read More

What is the simplest solution to flat a JavaScript array of objects into an object?

AmitDiwan
Updated on 03-Sep-2020 07:29:56

159 Views

Flat array of objects into an object, you can use the concept of reduce(). Let’s say following is our array of objects −const studentDetails = [    {Name: "Chris"},    {Age: 22} ]Exampleconst studentDetails = [    {Name: "Chris"},    {Age: 22} ] const objectStudent = studentDetails.reduce((obj, value) => {    return { ...obj, ...value } }, {}) console.log(objectStudent);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo64.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo64.js { Name: 'Chris', Age: 22 }

Possible to split a string with separator after every word in JavaScript

AmitDiwan
Updated on 03-Sep-2020 07:27:59

260 Views

To split a string with separator after every word, the syntax is as follows −var anyVariableName=yourVariableName.split('parameter').filter(value=>value)Let’s say, the following is our string with separator −var sentence="-My-Name-is-John-Smith-I-live-in-US";Now, split the string with separator as in the below code.Examplevar sentence="-My-Name-is-John-Smith-I-live-in-US"; console.log("The value="+sentence); var result=sentence.split('-').filter(value=>value) console.log("After split()="); console.log(result);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo63.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo63.js The value=-My-Name-is-John-Smith-I-live-in-US After split()= [    'My', 'Name',    'is', 'John',    'Smith', 'I',    'live', 'in',    'US' ]Read More

Length of a JavaScript associative array?

AmitDiwan
Updated on 03-Sep-2020 07:25:16

651 Views

You can use the length property from JavaScript to get the length. Let’s create an Associative array −var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript";Let us now get the length of the associative array −Examplevar details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript"; console.log("The length=="+Object.keys(details).length);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo.js The length==4Read More

Convert HH:MM:SS to seconds with JavaScript?

AmitDiwan
Updated on 13-Nov-2024 12:28:23

1K+ Views

To convert HH:MM:SS to seconds with JavaScript, we will be using simple arithmetic operations like mltiplication and addition. We will be understanding this conversion with two example codes and their stepwise explanation. In this article, we are given a time in HH:MM:SS format and our task is to convert HH:MM:SS to seconds with JavaScript. Steps to Convert HH:MM:SS to Seconds We have used two div to display the time and the result in seconds. The time variable stores the time in HH:MM:SS format. Then we have used split() function to ... Read More

How do I subtract one week from this date in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:21:43

3K+ Views

You need to subtract one week i.e. 7 days from the current date. Following is the syntax −var anyVariableName=new Date(yourCurrentDate.setDate(yourCurrentDate.getDate() - 7)At first, get the current date −var currentDate = new Date(); console.log("The current Date="+currentDate);Now, set the new date with setDate() method and subtract 7 days −Examplevar currentDate = new Date(); console.log("The current Date="+currentDate); var before7Daysdate=new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date="+before7Daysdate);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo60.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo60.js The current Date=Tue Jul 14 2020 19:12:43 GMT+0530 (India ... Read More

Advertisements