I want to create a Javascript array by adding the individual parts to the array using the push function.
This also works so far:
the creation of the array:
var buildProduktion = new Array();
and here the individual arguments are defined and added to the array via push:
var singleString = {step: 1, name: sName.value, description: sDescription.value, lon: sLon.value, lat: sLat.value, ficon: sIcon.value, img: filePathName}
buildProduktion.push(singleString)
I repeat this step with different data so that this array is created in the end:
[
{step: 1, name: sName.value, descri...},
{step: 2, name: sName.value, descri...},
{step: 3, name: sName.value, descri...},
{step: 4, name: sName.value, descri...},
{step: 5, name: sName.value, descri...},
{step: 6, name: sName.value, descri...}
]
but I would like to name the arrays like shown below:
"ExampleName":[
{step: 1, name: sName.value, descri...},
{step: 2, name: sName.value, descri...},
{step: 3, name: sName.value, descri...},
{step: 4, name: sName.value, descri...},
{step: 5, name: sName.value, descri...},
{step: 6, name: sName.value, descri...}
]
but I don't know how to do it with push or something like that...
The problem is actually relatively simple and has probably already been answered somewhere, but I just don't know the right keywords to look for
CodePudding user response:
Assuming your code is living inside an object named arrayObject.
let arrayObject = {
ExampleName:[]
}
arrayObject.ExampleName.push(singleString);
this will add strings to the Examplename array of arrayObject
I hope below standalone example code will make you understand better
let step = 'step'
let arrayObject = {
ExampleName: []
}
let singleString1 = { step1: 1 }
let singleString2 = { step2: 2 }
let singleString3 = { step3: 3 }
arrayObject.ExampleName.push(singleString1);
arrayObject.ExampleName.push(singleString2);
arrayObject.ExampleName.push(singleString3);
console.log(arrayObject);
CodePudding user response:
seems like you want to create a javascript object, and store the array inside. but this is not part of the array itself. all you need is:
const someObjName = {
"ExampleName":buildProduktion
}