How to combine a flat arrays into hierarchy in Javascript - javascript

say I have a data structure like this:
const flat = [
[
{
id: 0,
schema: "class",
name: "basement below baseLevel"
}
{
id: 1,
schema: "class",
name: "baseLevel"
},
{
id: 2,
schema: "TopLevelClass",
name: "The Top"
},
],
[
{
id: 3,
schema: "class",
name: "Second base level"
},
{
id: 2,
schema: "TopLevelClass",
name: "The Top"
}
]
]
The structure assumes a nested hierarchy where the root is the last element of each inner array. I need to restructure the data to look like this:
const root = [
id: 2,
schema: "TopLevelClass",
name: "The Top",
children: [
{
id: 1,
schema: "class",
name: "base Level",
children: [
{
id: 0,
schema: "class",
name: "Basement below base level"
}
]
},
{
id: 3,
schema: "class",
name: "Second base level"
}
]
]
There are some examples that combine arrays into hierarchies similar to this, but they always include either a parent or child id and the data structure I am working with does not. It just assumes (I know this is probably not a good assumption to be making, but its what I am working with) that each inner array is structured base up. Is there a concise way to nest the flattened array into a hierarchy?
Thanks!

Related

How to setState for nested array of object in react?

This is the object, lets say I'm gonna add something inside list: [], how can I do that? I know like we can do it using the prevList callback but I'm kind of confused to map through it.
const [boardlist, setBoardlist] = useState([
{
id: 1,
boardName: "home work",
data: [
{
id: 1,
header: "Stuff to do",
list: [
{
id: 1,
taskTitle: "working from home",
},
],
},
{
id: 2,
header: "In Progress",
list: [],
},
{
id: 3,
header: "Done",
list: [],
},
],
},
]);
What about make a copy of the state and just push using the bracket notation and dot notation to reach the element that you need to change?
const boardlistCopy = JSON.parse(JSON.stringify(boardlist));
boardlistCopy[0].data[1].list.push({id: 2, task: "studying React"});
setBoardlist(boardlistCopy);

How to walk a nested array of objects and apply a callback function to every element that changes it

I need to change the prop key names and prop values of all elements within a nested array of objects
Given this nested object array:
const nestedArr = [
{
id: 1,
name: "foo",
children: [
{
id: 2,
name: "bar",
children: [
{
id: 3,
name: "baz"
}
]
}
]
},
]
I want to be able to apply a callback function where I can mutate each object. For this example say I wanted to append all elements name prop with a "1". Or maybe I wanted to change the "name" prop key to "name1".
So after I run a function on it mutateRecursively(nestedArr, callback) the array should change to:
const nestedArr = [
{
id: 1,
name1: "foo1",
children: [
{
id: 2,
name1: "bar1",
children: [
{
id: 3,
name1: "baz1"
}
]
}
]
},
]
So basically I need to be able to recursively walk the tree and for each element run a callback function that takes the element as param, does something to it and returns the new mutated one.
I'm not so interested in the implementation details regarding the callback function, but more like how to walk the tree, and for each element run the callback function that returns a new modified instance of eac element.
Try this:
const nestedArr = [
{
id: 1,
name: "foo",
children: [
{
id: 2,
name: "bar",
children: [
{
id: 3,
name: "baz"
}
]
}
]
},
];
function applyCallbackRecursively(array, callback) {
return Array.isArray(array) ? array.map(({ children, ...item }) => ({ children: applyCallbackRecursively(children, callback), ...callback(item) })) : array;
}

MongoDB $in operator

I have a table with following values
[
{
id: 1,
name: "abc"
},
{
id: 2,
name: "lmn"
},
{
id: 3,
name: "xyz"
}
]
I have a query with $in as
{
id: {
$in: [ 2, 3, 1 ]
}
}
i want the output to come like so:
[
{
id: 2,
name: "lmn"
},
{
id: 3,
name: "xyz"
},
{
id: 1,
name: "abc"
}
]
But it does not come like that. Is there a way I could achieve this output?
You have to add a $sort stage, as $in does not preserve order.
There are quite a few questions on stackoverflow about this issue for example this question.

How to remove duplicated item in array of array

I have an array like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
},
{
name: 'Cancellation',
component: '/cancellation'
}
]
}]
and another like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
}
]
},
{
id: 3,
name: 'Example'
}]
My question is how is the best way to filter arrays, and remove one if have duplicated or more, even in array of children. Or the best, is like arrays merge!

How to use normalizr to flatten an array with different types of objects?

So, I get a JSON response from the server that looks something like:
{
data: [
{ id: 1, type: 'person', emails: [ { id: 1 }, { id: 3 } ], phones: [] },
{ id: 2, type: 'person', emails: [ { id: 2 } ], phones: [ { id: 2 } ] },
{ id: 3, type: 'person', emails: [ { id: 4 } ], phones: [ { id: 3 }, { id: 3 }] }
],
included: [
{ id: 1, type: 'emails', ... },
{ id: 2, type: 'emails', ... },
{ id: 3, type: 'emails', ... },
{ id: 4, type: 'emails', ... },
{ id: 1, type: 'phones', ... },
{ id: 2, type: 'phones', ... },
{ id: 3, type: 'phones', ... }
]
}
The data property is an array of contact objeccts all with the same structure. Each contact object has an array of related emails and phones.
The included property is an array of ALL types of related objects which means they can share and id or even have a difference object structure.
I'm looking to try and flatten the response to be easier to work with and resemble:
{
entities: {
contacts: [ ... ],
emails: [ ... ],
phones: [ ... ]
},
result: [ 1, 2, 3 ]
}
I've managed to normalize just the contact data using:
const contactSchema = new schema.Entity('contacts');
const contactListSchema = [ contactSchema ];
const normalizedData= normalize(response, contactListSchema);
But that obviously won't include the emails or phones in the entities.
I don't actually know if this library is capable of what I'm trying to achieve, but any help would be appreciated.
While not based on the data above, the API is based off of the jsonapi.org schema, so the example on the homepage matches exactly with the structure.
I actually found a library specifically designed to do this based on the original normalizr:
https://github.com/stevenpetryk/jsonapi-normalizer
Hope this may help someone in the future!

Categories