1

There are 2 variables:

var brands = 
[
    {Brand: "Hyundai"},
    {Brand: "Toyota"},
    {Brand: "Honda"},
    {Brand: "Volkswagen"}
]

var cars =[
    {Car: "Elantra", BrandIndex = 0},
    {Car: "Tucson", BrandIndex = 0},
    {Car: "Creta", BrandIndex = 0},
    {Car: "Verna", BrandIndex = 0},
    {Car: "Camry", BrandIndex = 1},
    {Car: "Fortuner", BrandIndex = 1},
    {Car: "Corolla", BrandIndex = 1},
    {Car: "Civic", BrandIndex = 2},
    {Car: "CR-V", BrandIndex = 2},
    {Car: "City", BrandIndex = 2},
    {Car: "Polo", BrandIndex = 3},
    {Car: "Passat", BrandIndex = 3},
    {Car: "Beatle", BrandIndex = 3},
    {Car: "Vento", BrandIndex = 3}
]

BrandIndex is the index of the Brand variable items.

The code is written as such that when a Brand is deleted (spliced from Brands variable), its corresponding car are spliced as well which is identified by BrandIndex.

Now the issue is that we need to update the BrandIndex of the other cars. eg.

If Toyota is deleted, the cars variable looks like

cars =[
    {Car: "Elantra", BrandIndex = 0},
    {Car: "Tucson", BrandIndex = 0},
    {Car: "Creta", BrandIndex = 0},
    {Car: "Verna", BrandIndex = 0},
    {Car: "Civic", BrandIndex = 2},
    {Car: "CR-V", BrandIndex = 2},
    {Car: "City", BrandIndex = 2},
    {Car: "Polo", BrandIndex = 3},
    {Car: "Passat", BrandIndex = 3},
    {Car: "Beatle", BrandIndex = 3},
    {Car: "Vento", BrandIndex = 3}
]

How do we update the brand Index of Honda as 1 and Volkswagen as 2 for all the cars on deletion?

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.