Found 9321 Articles for Object Oriented Programming

Sort array by month-year JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:11:10

599 Views

Suppose, we have an array that contains dates in MM-YYYY format like this −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the array are arranged in oldest to newest order.ExampleThe code for this will be −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; const padToString = (num) => {    return String("0" + num).slice(-2); }; const sortByDate = (first, second) => ... Read More

Find the most frequent number in the array and how many times it is repeated in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:09:48

227 Views

We are required to write a JavaScript function that takes in an array of literals and finds the most frequent number in the array and how many times it is repeated.ExampleThe code for this will be −const arr = ['13', '4', '1', '1', '4', '2', '3', '4', '4', '1', '2', '4', '9', '3']; const findFrequency = (arr = []) => {    const count = {};    const max = arr.reduce((acc, val, ind) => {       count[val] = (count[val] || 0) + 1;       if (!ind || count[val] > count[acc[0]]) {          return ... Read More

JavaScript - Determine all possible ways a group of values can be removed from a sequence

AmitDiwan
Updated on 23-Nov-2020 10:08:22

62 Views

We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance value each from the original sequence.For example − If the sequence array is −const arr = [1, 2, 1, 3, 1, 4, 4];And the array to be removed is −const arr2 = [1, 4, 4];Then there are three possible ways of doing this without disrupting the order of elements −1 --> [2, 1, 3, 1] 2 --> [1, 2, 3, 1] ... Read More

Filter nested object by keys using JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:06:07

854 Views

Suppose, we have an array of objects like this −const arr = [{ 'title': 'Hey',    'foo': 2,    'bar': 3 }, {    'title': 'Sup',    'foo': 3,    'bar': 4 }, {    'title': 'Remove',    'foo': 3,    'bar': 4 }];We are required to write a JavaScript function that takes in one such array as the first input and an array of string literals as the second input.Our function should then prepare a new array that contains all those objects whose title property is partially or fully included in the second input array of literals.ExampleThe code for ... Read More

Convert array with duplicate values to object with number of repeated occurrences in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:04:40

619 Views

Suppose, we have an array string that contains some duplicate entries like this −const arr = ['California', 'Texas', 'Texas', 'Texas', 'New York', 'Missouri', 'New Mexico', 'California'];We are required to write a JavaScript function that takes in one such array. Our function should then construct an array of objects that contains an object for each unique entry and a property "count" that contains its count in the original array.Therefore, the final output for the above array should look something like this −const output = [    {'name':'California', 'count':2},    {'name':'Texas', 'count':3},    {'name':'New York', 'count':1},    {'name':'Missouri', 'count':1},    {'name':'New Mexico', ... Read More

Search from an array of objects via array of string to get array of objects in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:02:57

294 Views

Suppose, we have one array of strings and another array of objects like this −const arr1 = [ '1956888670', '2109171907', '298845084' ]; const arr2 = [    { KEY: '1262875245', VALUE: 'Vijay Kumar Verma' },    { KEY: '1956888670', VALUE: 'Sivakesava Nallam' },    { KEY: '2109171907', VALUE: 'udm analyst' },    { KEY: '298845084', VALUE: 'Mukesh Nagora' },    { KEY: '2007285563', VALUE: 'Yang Liu' },    { KEY: '1976156380', VALUE: 'Imtiaz Zafar' }, ];We are required to write a JavaScript function that takes in two such arrays. Then our function should return a filtered version of the second ... Read More

Convert the number or boolean type JSON object from string type to its original in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:01:48

841 Views

Suppose we have a short JSON object like this −const obj = {"name":"sam", "age":"24", "isMarried":"false"};Here, some of the Number and Boolean values, by mistake, have been coerced to String.Like the age property which was a Number and isMarried property which was a boolean. Our job is to write a function that takes in one such object and correctly changes these incorrect data types with the correct ones.ExampleThe code for this will be −const obj = {    "name":"sam",    "age":"24",    "isMarried":"false" }; const convertToOriginal = (obj = {}) => {    const keys = Object.keys(obj);    for(let i = ... Read More

Grouping data to month wise in JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 18:50:12

718 Views

In the given problem statement we have to group the given data month wise. In simple terms the data should be in sorted form after grouping them month wise. For example we have years and months given in data so it should be visible in the order from January to December. As we know that Arrays are the data structures template to store data. And we can manipulate that data as per our requirement. It can store a collection of items in a single array, and has many operations to perform like adding an element, deleting an element, ... Read More

Convert number to characters in JavaScript

Nikitasha Shrivastava
Updated on 22-Aug-2023 14:24:43

4K+ Views

In the given problem statement we are asked to convert numbers to characters with the help of javascript functionalities. In Javascript we have some built−in functions to convert a number to its corresponding characters and also with user defined functions we can convert it. Logic for The Above Problem As we know that every programming language defines its own function to perform certain operations. So Javascript has a built−in method to convert a number into its corresponding character. These function names are 'fromCharCode' and 'charCodeAt'. With these functions we need to pass the input number and it will convert and ... Read More

Get value for key from nested JSON object in JavaScript

Nikitasha Shrivastava
Updated on 23-Aug-2023 12:42:59

14K+ Views

In the given problem statement we are asked to get value for key from nested JSON object with the help of javascript functionalities. In Javascript we can solve and access the json data object with the help of dot notation or bracket notation. Before starting coding for the given problem, let's understand what exactly is the JSON object: What is JSON and nested JSON objects? JSON’s full form is Javascript Object Notation. It is a standard text based format to show the structured data. It is basically used to transmit data in web applications, which means we can send ... Read More

Advertisements