Home > front end >  I want to have two json merge together
I want to have two json merge together

Time:10-31

I want to get a json


{" id ": 1," array1 ": 2, 'array2:3,' array3, 4},
{" id ": 2, 'array1:3,' array2:4},
{" id ": 3, 'array1:4,' array2:5, 'array3:6,' array4:7, 'array5:8}
]
Based on two different json id associated together,
JsonA:

[{" id ": 1}, {" id", 2}, {" id ": 3}]
JsonB:


{" id ": 1, 'array' : [4] 2},
{" id ": 2, 'array' : [3, 4]},
{" id ": 3, 'array' :,6,7,8 [5]}
]
How can I achieve?

CodePudding user response:

 
<script>
Var a=[{
"Id" : 1
}, {
'id' : 2
}, {
"Id" : 3
}]
Var b=[{
"Id" : 1,
'array' : [2, 3, 4]
},
{
"Id" : 2,
'array' : [3, 4]
},
{
"Id" : 3,
'array' : [5, 6, 7, 8]
}
]
Let arr=[]
A.f orEach (item=& gt; {
Var json={}
Var ob=b. ind (j=& gt; J.i d===item. Id)
Json. [' id ']=item id
Ob. Array. A forEach ((o, index)=& gt; {
Json [' array '+ + index (+)]=o
})
Arr. Push (json)
})
The console. The log (arr)
</script>

CodePudding user response:

JsonA: should be relatively simple,
JsonB: determine the length, then extracted,

If is me, and this stupid way,

CodePudding user response:

Oh... Do you want jsonA and jsonB, merged into one big...
As luo, determine length, circular stitching

CodePudding user response:

 
Var jsonA=[{" id ": 1, t: 1}, {" id" : 2, t: 2}, {3} "id" : 3, t:]

Var jsonB=[
{" id ": 1, 'array' : [4] 2},
{" id ": 2, 'array' : [3, 4]},
{" id ": 3, 'array' :,6,7,8 [5]}
]

The function of the merge (jsonB jsonA) {
Let the result=[]
For (let the item of jsonA) {
Let the found=jsonB. Find (each=& gt; Each. Id===item. Id)
Let obj=found. Array. Reduce ((obj, v, I)=& gt; {
Obj. [' id ']=item id
Obj [' array '+ (I + 1)]=v
Object. The assign (obj, item)
Return obj
}, {})
Result. Push (obj)
}
return result;
}

The console. The log (merge (jsonB jsonA))

CodePudding user response:

Merge array
Array concat (array1, array2);

CodePudding user response:

reference 1st floor jio can reply:
 
<script>
Var a=[{
"Id" : 1
}, {
'id' : 2
}, {
"Id" : 3
}]
Var b=[{
"Id" : 1,
'array' : [2, 3, 4]
},
{
"Id" : 2,
'array' : [3, 4]
},
{
"Id" : 3,
'array' : [5, 6, 7, 8]
}
]
Let arr=[]
A.f orEach (item=& gt; {
Var json={}
Var ob=b. ind (j=& gt; J.i d===item. Id)
Json. [' id ']=item id
Ob. Array. A forEach ((o, index)=& gt; {
Json [' array '+ + index (+)]=o
})
Arr. Push (json)
})
The console. The log (arr)
</script>


1/f, the answer is ok, but also easy to understand,
If A and B data is not necessarily possible pairing, the ob. Array. Add A judgment before the forEach line ob. Array & amp; & Ob. Array. A forEach

Directly if you want to make A ([{" id ": 1}, {" id", 2}, {" id ": 3}]) become the results you want, where the arr, json don't define, in ob. Array. A forEach direct operation in A [' array '+ + index (+)] can

As follows:
 
Const arr1=[{" id ": 1}, {" id", 2}, {" id ", 3}, {4} 'id' :];
Const arr2=[
{" id ": 1, 'array' : [2, 3, 4]},
{" id ": 2, 'array' : [3, 4]},
{" id ": 3, 'array' : [5, 6, 7, 8]}
]
Arr1. ForEach (v1=& gt; {
Let arrTemp=arr2. Find (v2=& gt; V2. Id===v1. Id);
/* *
* if you have a TypeScript, can use the optional chain optimization of the following statements written
* arrTemp? The array? The forEach ()
*/
ArrTemp & amp; & ArrTemp. Array & amp; & ArrTemp. Array. ForEach ((v3, independence idx=& gt; {
(v1 [' array '+ + + independence idx)]=v3;
});
})
The console. The log (JSON. Stringify (arr1, null, 2));
  • Related