怎么做反向拆分

时间:2017-11-20 11:03:32

标签: javascript jquery

这里我有两个变量(值),在这两个我想要的值中:

  1. countryName
  2. stateName
  3. cityName
  4. localityName
  5. sublocalityName
  6. 所以我使用的是split(),但它工作不正常,第一个字符串工作正常,但第二个字符串无法正常工作,我认为反过来我们已拆分字符串,我们必须记录所有这些值,但我不知道该怎么做?

    
    
    var address = "8th Main Rd, Marathahalli Village, Marathahalli, Bengaluru, Karnataka 560037, India";
    
    var resAddress = address.split(",");
    var sublocalityNameTrim = resAddress[1];
    var sublocalityName = sublocalityNameTrim.trim();
    var localityNameTrim = resAddress[2];
    var localityName = localityNameTrim.trim();
    var cityNameTrim = resAddress[3];
    var cityName = cityNameTrim.trim();
    var stateAndPincode = resAddress[4];
    var countryTrim = resAddress[5];
    var countryName = countryTrim.trim();
    var sc = stateAndPincode.trim();
    var stateAndPincodeSplid = sc.split(" ");
    var stateNameTrim = stateAndPincodeSplid[0];
    var stateName = stateNameTrim.trim();
    var pincode = stateAndPincodeSplid[1];
    
    console.log("countryName = " + countryName);
    console.log("stateName = " + stateName);
    console.log("cityName = " + cityName);
    console.log("localityName = " + localityName);
    console.log("sublocalityName = " + sublocalityName);
    
    
    var address1 = "846, 2nd Main Rd, AECS Layout - D Block, AECS Layout, Marathahalli, Bengaluru, Karnataka 560037, India";
    
    var resAddress1 = address1.split(",");
    var sublocalityNameTrim = resAddress1[1];
    var sublocalityName = sublocalityNameTrim.trim();
    var localityNameTrim = resAddress1[2];
    var localityName = localityNameTrim.trim();
    var cityNameTrim = resAddress1[3];
    var cityName = cityNameTrim.trim();
    var stateAndPincode = resAddress1[4];
    var countryTrim = resAddress1[5];
    var countryName = countryTrim.trim();
    var sc = stateAndPincode.trim();
    var stateAndPincodeSplid = sc.split(" ");
    var stateNameTrim = stateAndPincodeSplid[0];
    var stateName = stateNameTrim.trim();
    var pincode = stateAndPincodeSplid[1];
    
    console.log("countryName = " + countryName);
    console.log("stateName = " + stateName);
    console.log("cityName = " + cityName);
    console.log("localityName = " + localityName);
    console.log("sublocalityName = " + sublocalityName);
    
    
    

3 个答案:

答案 0 :(得分:0)

请在下面找到有效的解决方案。我使用了Object而不是单个变量。您可以根据需要提取属性并分配给变量。

var address1 = "8th Main Rd, Marathahalli Village, Marathahalli, Bengaluru, Karnataka 560037, India";

var address2 = "846, 2nd Main Rd, AECS Layout - D Block, AECS Layout, Marathahalli, Bengaluru, Karnataka 560037, India"

function getAddObj(add) {
  var addArray = add.split(',').reverse().map(function(item) {
    return item.trim()
  })
  return addArray.reduce(function(acc, item, index) {
    switch (index) {
      case 0:
        acc.country = item;
        return acc;
      case 1:
        acc.state = item.split(' ')[0]
        return acc;
      case 2:
        acc.city = item;
        return acc;
      case 3:
        acc.locality = item;
        return acc;
      case 4:
        acc.subLocality = item;
        return acc;
      default:
        return acc;
    }
  }, {});
}

var countryName = getAddObj(address1).country;
var stateName = getAddObj(address1).state;

console.log(countryName);
console.log(stateName);

答案 1 :(得分:0)

invalidate()

答案 2 :(得分:0)

var address1 = "8th Main Rd, Marathahalli Village, Marathahalli, Bengaluru, Karnataka 560037, India";
var address2 = "846, 2nd Main Rd, AECS Layout - D Block, AECS Layout, Marathahalli, Bengaluru, Karnataka 560037, India"

function printObj(add) {
    var resAddress = add.split(',').reverse()
    console.log("countryName = " + resAddress[0].trim());
    console.log("stateName = " + resAddress[1].trim());
    console.log("cityName = " + resAddress[2].trim());
    console.log("localityName = " + resAddress[3].trim());
    console.log("sublocalityName = " + findSubLocality(resAddress));
}

function findSubLocality(data) {
    var subLocality = "";
    for (var i = data.length - 1; i >= 4; i--) {
        subLocality = subLocality + data[i] + ",";
    }
    subLocality = subLocality.substr(0, subLocality.length - 1);
    return subLocality;
}

printObj(address1);
printObj(address2);