I want to modify existing array which looks like this:
[
[
{
prop1: 'FieldName',
prop2: 'FieldValue'
}
],
[
{
prop1: 'OtherFieldName',
prop2: 'OtherFieldValue'
}
],
]
Into having an array of objects with each object now having an unique id and setting prop1
value as a key for my new object.
[
{
id: 1,
FieldName: 'FieldValue'
},
{
id: 2,
OtherFieldName: 'OtherFieldValue'
}
]
I know that this probably asks more than the original question, but I think I’m missing something very simple here, just don’t know the most optimal way to go about it.
2
Answers
Here is one solution.
Iterate the original array, create a new object in each iteration, add an
.id
for it, then iterate the element to get the rest key-value pairs:Try it (terser version):