I got these 2 arrays:
const data = [
{ url: 'google.com', title: 'google', social: false, corporate: true },
{ url: 'facebook.com', title: 'facebook', social: true, corporate: true }
];
const valuesToExtract = ['url', 'title'];
I need to get to this result:
const result = [
{ url: 'somepath[0].url', title: 'somepath[0].title' },
{ url: 'somepath[1].url', title: 'somepath[1].title' }
]
I tried this way:
const newArray = [];
for (let i = 0; i < data.length; i++) {
for (const value of valuesToExtract ) {
const newObject = {};
newObject[value] = `somepath[${i}].${value}`;
newArray.push(newObject);
}
}
But the result I get is:
const result = [
{ url: 'somepath[0].url' },
{ title: 'somepath[0].title' },
{ url: 'somepath[1].url' },
{ title: 'somepath[1].title' }
]
How can I do it better?