How to loop through an array of object got similar key and different value for Example if,
arrayOfObjects = [
{ parent: 'relatedParty', child: 'role === CEO', andOr = '&&' }
{ parent: 'relatedParty', child: 'name === Arun' , andOr = '||'}
{ parent: 'relatedParty', child: ' id === 123', andOr = '&&' }
{ parent: 'cusotm', child: 'contact === DAP', andOr = '||' }
{ parent: 'custom', child: 'team==dap', andOr = '&&' }
{ parent: 'multiple', child: 'key ===keys', andOr = '||' }
{ parent: 'multiple', child: 'value=== values', andOr = '&&' }
].
im stuck in a position where how can i iterate through this and get output like if
first object of parent is === second object, then I want them to add into an array and finally those values should be inside another array
[[role === CEO && name === Arun && id === 123 &&], [contact === DAP || team==dap && ],[ key ===keys || value=== values &&]]
2
Answers
Your example is not a valid array, are you getting it from somewhere else, like a http request?
Anyway, after fixing it to a valid array, you can try grouping by parent, and them mapping the values into what you need.
Grouping could be like:
You can also use reduce, or other ways to group by parent.
After it, just map the values into what you want:
If you need it in string format to send to some api, do it like:
Here is a more concise way to handle your problem: