How can I turn the below array
['12345', '83747']
into the below array of objects
[ {'id': '12345'}, {'id': '83747'} ]
using map?
My attempt so far, iDs is an empty array, chunk is an array of string.:
obj.iDs.concat(
chunk.map((item) => ({
id: item,
})),
);
An example, my IDE reports no issues with this code:
const body = [{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'},{'id':'1234'}]
const batchGetRequestObj = {
ids: [],
targetProperties: ['contentID, updateDateTime'],
};
function func() {
try {
chunkArray(
body.map((item) => {
return item.id;
}),
25,
).forEach((chunk) => {
batchGetRequestObj.ids.concat(
chunk.map((item) => ({
ids: item,
})),
);
console.log(batchGetRequestObj);
});
} catch (e) {
console.log(e);
}
}
function chunkArray(array: string[], size: number) {
const slicedArray = [];
for (let i = 0; i < array.length; i += size) {
slicedArray.push(array.slice(i, i + size));
}
return slicedArray;
}
chunkthen?