0

这是我需要的对象结构:

var states = {
    'countryCode1' : { 'stateCode1' : 'stateName1', 'stateCode2' : 'stateName2', .... }
    'countryCode2' : { 'stateCode1' : 'stateName1', 'stateCode2' : 'stateName2', .... }
    .................................
};

在这个循环中,countryCode、stateCode 和 stateName 有单独的值,如何构建具有上述结构的对象?

for (var loop = 0; loop < stateLineArray.length; loop++) {

    lineArray = stateLineArray[loop].split(":");
    countryCode = this.TrimString(lineArray[0]);
    stateCode = this.TrimString(lineArray[1]);
    stateName = this.TrimString(lineArray[2]);

    // How can I add the values countryCode, stateCode and stateName in the states object, respecting it's structure?

    // Spmething like this:
    states[countryCode] = ....;

}

有任何想法吗?

下面是 stateLineArray 的结构:

var stateLineArray = '\
Antigua and Barbuda:Antigua:Antigua|\
Antigua and Barbuda:Barbuda:Barbuda|\
Antigua and Barbuda:Redonda:Redonda|\
Antigua and Barbuda:Saint George:Saint George|\
Antigua and Barbuda:Saint John:Saint John|\
Antigua and Barbuda:Saint Mary:Saint Mary|\
Antigua and Barbuda:Saint Paul:Saint Paul|\
Antigua and Barbuda:Saint Peter:Saint Peter|\
Antigua and Barbuda:Saint Philip:Saint Philip|\
Argentina:Buenos Aires:Buenos Aires|\
Argentina:Catamarca:Catamarca|\
Argentina:Chaco:Chaco|\
Argentina:Chubut:Chubut|\
Argentina:Ciudad de Buenos Aires:Ciudad de Buenos Aires|\
Argentina:Córdoba:Córdoba|\
Argentina:Corrientes:Corrientes|\
Argentina:Entre Ríos:Entre Ríos|\
Argentina:Formosa:Formosa|\
Argentina:Jujuy:Jujuy|\
Argentina:La Pampa:La Pampa|\
Argentina:La Rioja:La Rioja|\
Argentina:Mendoza:Mendoza|\
Argentina:Misiones:Misiones|\
Argentina:Neuquen:Neuquen|\
Argentina:Río Negro:Río Negro|\
Argentina:Salta:Salta|\
Argentina:San Juan:San Juan|\
Argentina:San Luis:San Luis|\
Argentina:Santa Cruz:Santa Cruz|\
Argentina:Santa Fé:Santa Fé|\
Argentina:Santiago del Estero:Santiago del Estero|\
Argentina:Tierra del Fuego:Tierra del Fuego|\
Argentina:Tucuman:Tucuman|\
Bahamas:Abaco:Abaco|\
Bahamas:Acklins and Crooked Islands:Acklins and Crooked Islands|\
Bahamas:Andros Island:Andros Island|\
Bahamas:Bimini:Bimini|\
Bahamas:Cat Island:Cat Island|\
Bahamas:Eleuthera:Eleuthera|\
......'

谢谢!

4

3 回答 3

0

假设trimstring返回它应该返回的内容(如 OP 所说),这应该可以解决问题:

lineArray = stateLineArray[loop].split(":");
countryCode = this.TrimString(lineArray[0]);
stateCode = this.TrimString(lineArray[1]);
stateName = this.TrimString(lineArray[2]);

var obj = { stateCode : stateCode, stateName : stateName };
states[countryCode] = obj;
于 2013-09-24T20:47:36.007 回答
0
var states = []

var stateLineArray = stateLineArray.split("|\\");

for (var loop = 0; loop < stateLineArray.length; loop++) {

    lineArray = stateLineArray[loop].replace("\\", "", split(":"));
    countryCode = this.TrimString(lineArray[0]);
    stateCode = this.TrimString(lineArray[1]);
    stateName = this.TrimString(lineArray[2]);

    if (typeof(states[countryCode]) == 'undefined') {

         states[countryCode] = [];

    }

    states[countryCode][statecode] = stateName;

}
于 2013-09-24T20:48:02.867 回答
0

关键是创建一个新对象来保存尚未定义的每个 countryCode。请注意,我假设每个 stateCode 都是唯一的。

var states = {};
for (var loop = 0; loop < stateLineArray.length; loop++) {

    lineArray = stateLineArray[loop].split(":");
    countryCode = this.TrimString(lineArray[0]);
    stateCode = this.TrimString(lineArray[1]);
    stateName = this.TrimString(lineArray[2]);

    // How can I add the values countryCode, stateCode and stateName in the states object, respecting it's structure?

    states[countryCode] = states[countryCode] || {};  //create new object if it does not exist
    states[countryCode][stateCode] = stateName;
}
于 2013-09-24T20:50:41.657 回答