0

I want to cycle through an array of strings and split those strings on a certain character, and then feed those new strings into an array, ex. take the string "val: key" from the first array and then use a function to retrieve the string from the array and split it on ":" into a new array that would contain ["val","key"]. This is what I have so far, and at the bottom is what is returning when console.log it.

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"
]

Why is it not splitting into two strings on the ";", and why is it returning an undefined value?

2
  • 1
    dictFinal.push(...) or dictFinal[i] = ... Commented Mar 18, 2015 at 16:19
  • 1
    arr.toString(); makes no sense Commented Mar 18, 2015 at 16:22

3 Answers 3

3

It is undefined because you are doing += to an empty array index

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

First pass dictFinal[i] is undefined so it is

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

You probably just want

dictFinal[i] = arr[i].split(";");
Sign up to request clarification or add additional context in comments.

Comments

0

Use dictFinal.push(...) or dictFinal[i] = ...

Calling arr.toString(); doesn't do much in your case; it simply makes a string from the array which is then expected to be assigned to an variable / returned etc...


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"]]

+= will try to get whatever is in the variable and concat / add with whatever is on the right side of the equal sign:

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

Comments

0

if you really need the += and would not want to see the 'UNDEFINED', could try:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.