Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 45 of 589
How to merge two arrays with objects in one in JavaScript?
Suppose, we have two arrays of objects like these − const arr1 = [ {name:'test', lastname: 'test', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [ {name:'test21', lastname: 'test21', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'}, {name:'test22', lastname: 'test22', gender:'m'} ]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ { name: 'test', lastname: 'test', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { ...
Read MoreRegroup JSON array in JavaScript
Suppose, we have a JSON array of objects like this − const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", ...
Read MoreCheck how many objects are in the array with the same key in JavaScript
Suppose, we have an array of objects containing some data about some users like this − const arr = [ { "name":"aaa", "id":"2100", "designation":"developer" }, { "name":"bbb", "id":"8888", "designation":"team lead" }, { "name":"ccc", "id":"6745", ...
Read MorePartition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript
We need to write a JavaScript function that partitions a number into chunks following specific rules: The number of chunks must be a power of 2 (1, 2, 4, 8, 16, etc.) Each chunk size must also be a power of 2, with a maximum limit Understanding the Problem Let's examine how different numbers can be partitioned: For number 8: [8] This works because we have 1 chunk (power of 2) with size 8 (power of 2). For number 9: [8, 1] This works because we have 2 chunks (power of 2), each ...
Read MoreFlat array of objects to tree in JavaScript
Converting a flat array of objects into a tree structure is a common requirement in web development. This involves organizing objects with parent-child relationships into a hierarchical format. The Data Structure We start with a flat array where each object has an id, name, and parentId. Objects with parentId: null are root nodes, while others are children. .parent, .child { cursor: pointer; ...
Read MoreConverting a comma separated string to separate arrays within an object JavaScript
When working with comma-separated strings containing hierarchical data, we often need to parse them into structured objects. This article shows how to convert a string like 'dress/cotton/black, dress/leather/red' into an organized object with arrays. The Problem Suppose we have a string like this: const str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james'; We need to create a JavaScript function that converts this into an object where each category becomes a key, and all related values are grouped into arrays: const output = { dress: ["cotton", "black", "leather", "red", "fabric"], ...
Read MoreCount number of entries in an object having specific values in multiple keys JavaScript
In JavaScript, you often need to count objects in an array that match specific criteria across multiple properties. This is commonly done using the filter() method combined with conditional logic. Suppose we have an array of objects representing trade data: const arr = [ {"goods":"Wheat", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; We need to count objects where the "from" property equals "USA" and the "to" property equals "INDIA". Using filter() and length ...
Read MoreHow to sort a JavaScript object list based on a property when the property is not consistent
When sorting JavaScript object arrays with inconsistent properties, you need a custom comparator function. This scenario commonly occurs when some objects have date values while others have null or undefined dates. The requirement is to display objects without dates at the top (sorted alphabetically by name), followed by objects with dates (sorted chronologically). Problem Overview Consider an array where some objects have date properties and others don't: const items = [ { name: "Charlie", date: "2024-03-15" }, { name: "Alice", date: null }, ...
Read MoreConvert 2d tabular data entries into an array of objects in JavaScript
Converting 2D tabular data into an array of objects is a common requirement when working with datasets. This transformation groups rows by a unique identifier and creates objects with dynamic properties. Problem Overview Suppose we have an array of arrays representing tabular data: const arr = [ ["Ashley", "2017-01-10", 80], ["Ashley", "2017-02-10", 75], ["Ashley", "2017-03-10", 85], ["Clara", "2017-01-10", 90], ["Clara", "2017-02-10", 82] ]; We need to transform this into an array of objects where each unique name becomes a ...
Read MoreBest way to flatten an object with array properties into one array JavaScript
When you have an object containing arrays as values, you often need to flatten all these arrays into a single array. This is a common operation when working with grouped data. const obj = { arr_a: [9, 3, 2], arr_b: [1, 5, 0], arr_c: [7, 18] }; console.log("Original object:", obj); Original object: { arr_a: [ 9, 3, 2 ], arr_b: [ 1, 5, 0 ], arr_c: [ 7, 18 ] } The goal is to merge all array values into one ...
Read More