I'm trying to create a JSON and I need to do the following:
var arreglo = [];
var sumar = () => {
var id = arreglo.length + 1;
arreglo.push({id: id});
console.log('Arreglo', arreglo)
};
sumar();
as Result prints: Arreglo [ { id: 1 } ]
.
Suppose that I execute the function twice for the example and
would have this array: [ { id: 1 }, { id: 2 } ]
.
What I want to achieve is to be able to add an array to where the id: 1
or the id
that goes generating, achieved that is as follows:
[ {"id": 1 ,
"preguntas":[{
"idp":1,
"preg": "¿como estás?"
},{
"idp":2,
"preg": "it is important make my laptop?"
}]},
{ "id": 2,
"preguntas":[{
"idp":1,
"preg": "¿como estás?"
},{
"idp":2,
"preg": "it is important make my laptop?"
}]}
]
I know first what to fill another arrangement:
var sumarAobj = () => {
var id = arregloInterno.length + 1;
arregloInterno.push({
id: id,
pregunta: '¿como estás?'
});
...
};
How should I continue? I tried with push and splice but does not want to.
Thank you.