# Tag: data-structures

## Pass array in another array of objects

I have this nested array which I am trying to sum the number in the last arrays individually so it should return 400 for each array also trying to get the average of the total, for example if it is 400 then it is 100%, if 399 it will be 99.99%, the average needs to be from 100% and dynamic

## How to convert flat data structure to tree structure in JavaScript using recursion

I want to create a tree from the data provided using recursion. I am also trying to get the tree to pass an npm test, but when I run the test, it is failing. I am getting a tree, but it looks different than the one it is supposed to look like. Here is the code (with instructions in a

## How to combine the values which is array of the same property in the object’s array javascript

For the following array : I need a function that should merge arrays with the same keys in the object. Answer Try to use reduce

## JavaScript array Sort out! Indices problem

Given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array. e.g.A={4,5,3,7,1} After sorting the new array becomes A={1,3,4,5,7}. The required output should be “4 2 0 1 3” Answer You need to pair (tuple) the value with the original

## How to entirely level (map / reduce / recursion) a nested object of unknown depth in the most efficient manner?

I would like to do something like the following but in a more large scale and efficient way. Assume I have an array of objects where each object needs to be leveled/flattened. Convert something like this … … to that … As is shown above, address as well is an object which needs to be leveled/flattened. But most importantly, one

## How to add a property to every object in a nested JSON structure in JavaScript?

Let’s suppose I have an API response that looks like this: I want to transform it by adding the rid property to each object. How can I do so? I think there is some kind of recursion involved but I haven’t been able to draft a solution? Thanks. Answer You just need to iterate through object with recursive function. Try

## looping through an array of objects that contains three key, value pairs

I have these objects, which could be in an array, like in the example or a database: I want to subtract a total of { “points”: 5000 } as an example from the points values of these objects starting with the oldest points based on timestamp, keep in mind no payer’s points to go negative. then I want to return

## how to alter reduce function so it produce different result

I have this array: I want to transform that array into I have this code that doing about that: What it missing is ignoring the sections. Any ideas how to modify that reduce function so it will produce expected result? To determine a section I came up with this funciton: Answer I would use the same approach from my answer

## How to groupBy an Array of Objects and convert the duplicates

Hello to all i have this data structure that i need to group and if there are any duplicates to convert those two objects it in one line.. Data Structure Grouped function I have groupedBy address attrube by this function above by address My new data-structure is My final Result wanna be like this : Shipping Address for Shipment 1

## How would you generate missing data using single loop from an array which can contain from 0 to 7 elements?

So I have an API which returns me the value of water dispensed for the last 7 days and it can contain an empty array or a value such as: Assuming today is Thursday and I need an array of objects which would fill missing last 7 days data as: How would you do this considering that you need to