Question: Question:
I would like to merge the following two objects with javascript.
Since the values of val are common, the result is "A", and since the values of child are different, I want to keep both values and merge them. What kind of implementation can be considered?
The library uses jQuery.
Merge source
obj1 = {
val : "A",
child : [{
child_val: 'B',
}]
}
obj2 = {
val : "A",
child : [{
child_val: 'C',
}]
}
obj3 = {
val : "D",
child : [{
child_val: 'B',
}]
}
Expected results when merging obj1 and obj2
obj = {
val : "A",
child : [{
child_val: 'B',
},{
child_val: 'C',
}]
}
Expected results when merging obj1, obj2, obj3
obj =
[{
val : "A",
child : [{
child_val: 'B',
},{
child_val: 'C',
}]
},
{
val : "D"
child : [{
child_val: 'B',
}]
}]
postscript:
I've simplified it for the sake of explanation, but it may have been confusing.
Excuse me.
What I wanted to do was to get multiple directory paths and create a hierarchical structure like obj below.
Assuming that multiple paths are stored in the array as shown below
[
"A/B/C/D/E",
"A/B/C/D",
"A/B/D",
"B/B/C/D"
]
Finally convert to obj structure
obj =
[
{
val : "A",
child : [{
val : "B",
child : [
{
val : "C",
child : [{
val : "D",
child : [{
val : "E"
}]
}]
},
{
val : "D"
}
]
}]
},
{
val : "B",
child : [{
val : "B",
child : [
{
val : "C",
child : [{
val : "D"
}]
}
]
}]
}
]
In order to realize the above, I created a hierarchical structure for each path, and when I finally tried to merge, I stumbled and asked a question.
Answer: Answer:
The purpose is to get multiple directory paths and create a hierarchical structure like obj below.
In that case, it is better to "create the desired structure from the beginning" instead of "objectizing and merging" individually. The basic strategy at that time is to create a directory, and the code I actually tried is as follows.
var input = [
"A/B/C/D/E",
"A/B/C/D",
"A/B/D",
"B/B/C/D"
];
function addPath(root, path) {
var items = path.split("/");
var currDir = { child: root };
for (var i = 0; i < items.length; ++i) {
// 現在の階層に、目的の名前のサブ階層があれば移動、なければ作って移動
var item = items[i];
var dir = findDir(currDir, item);
if (dir != null) {
currDir = dir;
continue;
}
currDir = makeDir(currDir, item);
}
}
// 現在の階層から目的の名前を持ったサブ階層を探して返す。
// 線形検索なので効率はお察し
function findDir(curr, name) {
if (curr.child == null) {
return null;
}
for (var i = 0; i < curr.child.length; ++i) {
var dir = curr.child[i];
if (dir.val == name) {
return dir;
}
}
return null;
}
// 現在の階層に、サブ階層を作って返す。
function makeDir(curr, name) {
if (curr.child == null) {
curr.child = [];
}
var dir = { val: name };
curr.child.push(dir);
return dir;
}
var result = [];
for (var i = 0; i < input.length; ++i) {
addPath(result, input[i]);
}
console.log(JSON.stringify(result, null, 2));
The point of this method is both the findDir
and makeDir
functions. addPath
is created by defining these two functions with the desired structure as a directory. However, this implementation is somewhat inefficient. If you add a large number of paths, please consider that and improve it.