I am trying to write a function that allows me to dynamically add a number of entries to an object using the code below:
const colNumber = (number) => {
let columns = [];
for (let i = 0; i < number; i++) {
columns[i] = `'column-${i+1}' : {id: 'column-${i+1}', taskIds: [],}`
}
return columns
}
const getColNum = colNumber(5);
const initialData = {
tasks: {
'task-1': { id: 'task-1', content: 'Take out the garbage' },
'task-2': {id: 'task-2', content: 'Watch my favorite show'},
'task-3': {id: 'task-3', content: 'Charge phone'},
'task-4': {id: 'task-4', content: 'Cook dinner'},
},
columns: {
'column-0': {
id: 'column-0',
title: 'To do',
taskIds: ['task-1','task-2','task-3','task-4',]
},
getColNum,
},
columnOrder: ['column-0'],
colNumber: getColNum,
}
I have tried .join() and the spread operator but I'm still not getting the output I'm looking for.
output with code as it is above:
id: 'column-0',
title: 'To do',
taskIds: ['task-1','task-2','task-3','task-4',]
},getColNum: Array(5)
0: "'column-1' : {id: 'column-1', taskIds: [],}"
1: "'column-2' : {id: 'column-2', taskIds: [],}"
2: "'column-3' : {id: 'column-3', taskIds: [],}"
3: "'column-4' : {id: 'column-4', taskIds: [],}"
4: "'column-5' : {id: 'column-5', taskIds: [],}
with .join()
columns: 'column-0': {id: "column-0", title: "To do", taskIds: Array(4)}
getColNum: "'column-1' : {id: 'column-1', taskIds: [],},'column-2' : {id: 'column-2', taskIds: [],},'column-3' : {id: 'column-3', taskIds: [],},'column-4' : {id: 'column-4', taskIds: [],},'column-5' : {id: 'column-5', taskIds: [],}"
with spread
columns: 0: "'column-1' : {id: 'column-1', taskIds: [],}"
1: "'column-2' : {id: 'column-2', taskIds: [],}"
2: "'column-3' : {id: 'column-3', taskIds: [],}"
3: "'column-4' : {id: 'column-4', taskIds: [],}"
4: "'column-5' : {id: 'column-5', taskIds: [],}"
column-0: {id: "column-0", title: "To do", taskIds: Array(4)}