I probably need to use other data structure, but I'm stuck with this solution for now. Will appreciate any advice on this.
For now I have this data structure:
const data = [
{
id: 'node-1',
type: 'text',
data: 'Hello,'
},
{
id: 'node-2',
type: 'text',
data: [
{
id: 'node-3',
type: 'text',
data: ' world.'
}
]
},
{
id: 'node-4',
type: 'text',
data: [
{
id: 'node-5',
type: 'text',
data: [
{
id: 'node-6',
type: 'text',
data: 'Foo bar'
}
]
}
]
}
]
I need a function which will give next result:
function split(arr, start_node, start_index, end_node, end_index) { ... }
const { before, range, after } = split(data, 'node-3', 3, 'node-6', 3)
// before
[
{
id: 'node-1',
type: 'text',
data: 'Hello,'
},
{
id: 'node-2',
type: 'text',
data: [
{
id: 'node-3',
type: 'text',
data: ' wo'
}
]
},
]
// range
[
{
id: 'node-2',
type: 'text',
data: [
{
id: 'node-3',
type: 'text',
data: 'rld.'
}
]
},
{
id: 'node-4',
type: 'text',
data: [
{
id: 'node-5',
type: 'text',
data: [
{
id: 'node-6',
type: 'text',
data: 'Foo'
}
]
}
]
}
]
// after
[
{
id: 'node-4',
type: 'text',
data: [
{
id: 'node-5',
type: 'text',
data: [
{
id: 'node-6',
type: 'text',
data: ' bar'
}
]
}
]
}
]
The problem is to keep nesting structure and to do that effectively. The only solution I came up with was to make this in three different loops, but that obviously ineffective
split()? I don't quite understand your expected outputs.start_indexandend_indexparameters? How would they affect the result?