【问题标题】:Parse an array of strings into a new array javascript将字符串数组解析为新数组javascript
【发布时间】:2015-03-18 16:16:52
【问题描述】:

我想循环遍历一个字符串数组并将这些字符串拆分为某个字符,然后将这些新字符串输入一个数组,例如。从第一个数组中取出字符串“val:key”,然后使用一个函数从数组中检索字符串,并将其在“:”上拆分为一个包含 ["val","key"] 的新数组。这是我到目前为止所拥有的,底部是 console.log 时返回的内容。

var dict = [];
dict.push("me; pro: myself");
dict.push("labor, laboris; n: work");
dict.push("voco, vocare, vocavi, vocatum; v: call");

function parseDict(arr){
/* parseDict takes a dictionary entry and splits it between its latin  part and its part of speech/english translation*/
  var dictFinal = [];
  arr.toString();
  for (i = 0; i < arr.length; i++){
    dictFinal[i] += arr[i].split(";");
  }
  return dictFinal;
}

console.log(parseDict(dict)) prints out 
[ 0: "undefinedme; pro: myself"
  1: "undefinedlabor, laboris; n: work"
  2: "undefinedvoco, vocare, vocavi, vocatum; v: call"
]

为什么它没有在“;”上分成两个字符串,为什么它返回一个未定义的值?

【问题讨论】:

  • dictFinal.push(...)dictFinal[i] = ...
  • arr.toString(); 没有意义

标签: javascript arrays


【解决方案1】:

这是未定义的,因为您正在对空数组索引执行+=

dictFinal[i] += arr[i].split(";");
             ^^

第一次通过 dictFinal[i] 未定义,所以它是

dictFinal[i] = undefined + arr[i].split(";");

你可能只是想要

dictFinal[i] = arr[i].split(";");

【讨论】:

    【解决方案2】:

    使用dictFinal.push(...)dictFinal[i] = ...

    调用arr.toString(); 在您的情况下没有多大作用;它只是从数组中创建一个字符串,然后将其分配给变量/返回等...


    var dict = [];
    dict.push("me; pro: myself");
    dict.push("labor, laboris; n: work");
    dict.push("voco, vocare, vocavi, vocatum; v: call");
    
    function parseDict(dict) {
      // considered that you know the final length of the final
      //   length of the array you could use: new Array(dict.length)
      var dictFinal = []; 
      for (i = 0; i < dict.length; i++) {
        dictFinal[i] = dict[i].split(";");
      }
      return dictFinal;
    }
    
    console.log(parseDict(dict)); // [["me"," pro: myself"],["labor, laboris"," n: work"],["voco, vocare, vocavi, vocatum"," v: call"]]
    

    += 将尝试获取变量中的任何内容,并与等号右侧的内容进行连接/添加:

    var a = 'abc';
    a += 'def';
    console.log(a); // abcdef
    
    var b = 1;
    b += 1;
    console.log(b); // 2
    
    // and your's case:
    var c; // here c === undefined (your case dictFinal[i] === undefined)
    c += 'ABC';
    console.log(c); // undefinedABC
    
    var my_array = [1,2,3];
    // .toString() is called on the array so the concatenation can happen:
    // It would be the same as writing:
    //    'my_string' + my_array.toString();
    // or 'my_string' + my_array.join(','); 
    var d = 'my_string' + my_array;
    console.log(d); // my_string1,2,3
    

    【讨论】:

      【解决方案3】:

      如果您确实需要 += 并且不想看到“未定义”,可以尝试:

      dictFinal[i] = ((typeof dictFinal[i]!=='undefined') ? dictFinal[i]+arr[i].split(";") : arr[i].split(";"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-15
        • 2011-08-17
        • 2021-11-05
        • 2017-02-02
        • 1970-01-01
        • 2020-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多