使用自定义结构在对象内添加for循环中的值

时间:2013-09-24 20:41:00

标签: javascript

这是我需要的对象结构:

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|\
......'

谢谢!

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;

}

答案 2 :(得分:0)

关键是创建一个新对象来保存每个国家/地区代码(如果尚未定义)。请注意,我假设每个州的代码都是唯一的。

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;
}