How does the front end change to the format that the back end wants?

there is such a data at the front end:

this.regions:[1,2];
this.citys:["shanghai","chongqing"];

format required by backend:

area:[
    {
        regions:1,
        citys:"shanghai"
    },
    {
        regions:2,
        citys:"chongqing"
    }
]

in that case, how to change the front end?

< hr >

there is another case:
citys and regions are multiple selections, and the length may be inconsistent:

this.citys:["shanghai","chongqing","changsha","hunan"];

regions data may be small:

this.regions:[1];

what should I do if the length of the two is different?


what should I do if the length of the two is different?

from the question raised by the landlord, the backend thinks that there is a corresponding relationship between region and city , regardless of whether it is one-to-one or one-to-many. The front end thinks that region is not related to city .

so this should be a matter of communicating and understanding needs, right?


the simplest is the for loop.

var area = [];
var length = Math.max(length_1, length_2);

for (var i = 0; i < length; PPi) {
  area.push({
    regions: regions[i],
    cities: cities[i],
  });
}

what you can't get is undefined,. You can change it on my basis. And the plural of city is cities

Menu