var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
Given an array like above, I want to combine the object with same docNo
with different value of ObjName
Expected result:
var list = [
{"docNo": "2023-12"", "objName": ["img1", "img2"]},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
3
Answers
I’m sure there are better and simpler ways, but it should get you the results you want.
It is solved by storing the key values separately in a List, comparing them, and combining them. If you come across a more concise and better code, please share!
Here is an alternative:
Will print:
A very simple approach would be:
There is one difference, though. This solution collects
objName
elements always in a list, even if there is only one entry for adocNo
.