简体   繁体   中英

Array with defined key and multidimensional

I've looked at other similar posts with no help, they all start with a multidimensional array already made, I want to make one by using the .push method

My array is:

ItemsArray.push({ 
    BMW:{Model:X5,Model:X6,Model:X3 },
    Range Rover:{Model:Sports,Model:Venar}
});

The car make is defined by buttons so if the user clicks Fiat then the list generates models such as {Model: Punto} and pushes into the above array with Fiat:{Model: Punto} .

I tried using: ItemsArray[CarName].Models.push but it gave error

ItemsArray[CarName].Item' is not defined

Part of the code you provided will never work. This for instance: BMW:{Model:X5,Model:X6,Model:X3 } . It constructs an object and you're adding the same property, Model , three times. The result is this object: { Model: X3 } . Instead of an object you would have to use an array for this, like I did in my example below.

Besides that, using a array to store all data in is kind of impractical. You would get data structure like this:

 [ { brand: 'BMW', models: [ name: 'X5' ] }, { brand: 'Fiat', models: [ name: '500' ] }, ]

Now whenever you want to add a model to a brand, you first have to look for an entry in the array where the brand property matches the brand you want to add a model to. Only after doing this can you start manipulating the models for the brand.

I think that what you want to do is have an object which has a property per brand. Under this property you can store an array with models for that brand. I've mocked something up in the following snippet.

 const // This object will get an property per brand. modelsPerBrand = {}, // An array with the brand we support. brands = ['BMW', 'Ford', 'Fiat']; /** * This method will add a single model to a single brand. */ function addModelToBrand(brand, model) { const vehicles = modelsPerBrand[brand]; if (vehicles === undefined) { // Log a message or alternatively add the new brand to the modelsPerBrand object. console.log(`Unknown brand "${ brand }"`); return; } // Add the provided model to the array of models for the brand. vehicles.push({ name: model }); } /** * This method makes it easy to add multiple models at once to a * single car brand. */ function addModelsToBrand(brand, models) { if (modelsPerBrand[brand] === undefined) { console.log(`Unknown brand "${ brand }"`); return; } models.forEach(model => addModelToBrand(brand, model)); } // Create an empty array for each brand. It will result in an object // like this: // { // BMW: [], // Ford: [], // Fiat: [] // } brands.forEach(brand => modelsPerBrand[brand] = []); console.log('Object with brands: ', modelsPerBrand); // Add a single model to a brand. The object we have will now look // like this: // { // BMW: [ { name: 'X5' } ], // Ford: [], // Fiat: [] // } addModelToBrand('BMW', 'X5'); console.log('Object after adding BMW X5: ', modelsPerBrand); // Add a multiple models to a brand addModelsToBrand('Fiat', ['500', '500L']); console.log('Object after adding Fiat 500 and 500L: ', modelsPerBrand); // Add model to non-existing brand addModelsToBrand('KIA', 'Stinger'); // List all models for a brand: modelsPerBrand.Fiat.forEach(model => console.log(`Fiat ${ model.name }`));

Alternatively you could use a Map instead of an object for the vehiclesPerBrand variable but I wasn't sure how you were planning on using it. That's why I went with an Object instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM