Javascript
In a constructor and I want to manipulate it and want to add as many ids and content as much as I want.
var groups = new vis.DataSet([
{ id: 1, content: "" },
{ id: 2, content: "" },
{ id: 3, content: "" },
{ id: 4, content: "" },
]);
I am not 100% sure what you're asking for, so here are two solutions.
As Insyri mentioned in the comments, you should utilize the spread syntax. You could do something along the lines of this:
var groups = [
{ id: 1, content: 'abc' },
{ id: 2, content: 'cba' },
{ id: 3, content: '123' },
{ id: 4, content: '321' },
];
class someClass {
constructor(...data) {
this.key = data;
}
}
const test = new someClass(...groups);
console.log(test)
The output of the console log will look like this:
someClass {
key: [
{ id: 1, content: 'abc' },
{ id: 2, content: 'cba' },
{ id: 3, content: '123' },
{ id: 4, content: '321' }
]
}
If you want all of the objects to have a unique key (of whatever you want), you can do this:
class otherClass {
constructor(...data) {
data.forEach(obj=>{
this[obj.id] = obj
})
}
}
const test2 = new otherClass(...groups);
console.log(test2)
Output:
otherClass {
'1': { id: 1, content: 'abc' },
'2': { id: 2, content: 'cba' },
'3': { id: 3, content: '123' },
'4': { id: 4, content: '321' }
}