{" 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 arrayArray concat (array1, array2);
CodePudding user response: