0

I have a 2D array containing student profiles. Where Column 1 contains First Name, Col2 contains Middle Name, Col3 Last Name and so forth.

What I'm hoping to do is joining text from all three column, convert to upper case, then place back onto Column 1 i.e "FIRSTMIDDLELAST" in cell A1.

My snippet of code is below seems straight forward enough but I'm only very new to JS, upon executing I get an error..

Cannot read property "0" from undefined.

@ line

StudentList[i][0].concat(FistName,MidlName,LastName);

for (var i=0; i<StudentList.length; i++){
      var FistName = StudentList[i][0].valueOf().toUpperCase();
      var MidlName = StudentList[i][0].valueOf().toUpperCase();
      var LastName = StudentList[i][0].valueOf().toUpperCase();

       StudentList[i][0].concat(FistName,MidlName,LastName);
    }    

Some help would be greatly appreciated. Thank you so much advance.

UPDATE:

This is a small sample of the Google Spreadsheet where I my array came from ....

First   Middle   Last     Age  Grade  English  Maths
John    Peter    Smith    17   12     A        A
Kevin   Paul     James    16   11     B        C
Kim     Caroline Anderson 15   10     B        A
.... so on
2
  • seems your second column is null, put the command debugger; before var FistName = StudentList[i][0].valueOf().toUpperCase();, open your DevTools and check using the console. while the breakpoint is stopped... also seems you're taking the same column for all three names... your should change your StudentList[i][0] for StudentList[i][1] and [2] Commented Feb 18, 2017 at 6:19
  • 3
    Can you include example of array at Question? What is purpose of .valueOf() call? Commented Feb 18, 2017 at 6:20

2 Answers 2

1

the code seems fine, maybe you had wrong array value?

var StudentList = [];
StudentList.push(['First', 'Middle', 'Last']);

document.write('original: ' + StudentList.toString() + '<br/>');
for (var i=0; i<StudentList.length; i++){
  var FistName = StudentList[i][0].valueOf().toUpperCase();
  var MidlName = StudentList[i][1].valueOf().toUpperCase();
  var LastName = StudentList[i][2].valueOf().toUpperCase();


  //StudentList[i][0].concat(FistName,MidlName,LastName);
  StudentList[i][0] = FistName.concat(MidlName,LastName); //i think you mean this
};
document.write('modified: ' + StudentList.toString());

Sign up to request clarification or add additional context in comments.

1 Comment

your method works without error!. So why then does StudentList[i][0] = FistName.concat(MidlName,LastName); works yet StudentList[i][0] = FistName + MidlName + LastName; doesn't, so we can learn for future reference.
0

Assuming your structure is like this (as you described):

var StudentList = [
  ['fname1','mname1','lname1'],
  ['fname2','mname2','lname2'],
  ['fname3','mname3','lname3']
];

Your code could be something like this:

for (var i = 0; i < StudentList.length; i++) {
  var FistName = StudentList[i][0].toUpperCase();
  var MidlName = StudentList[i][1].toUpperCase();
  var LastName = StudentList[i][2].toUpperCase();

  StudentList[i][0] = FistName + MidlName + LastName;
}

/*
[
  [
    "FNAME1MNAME1LNAME1",
    "mname1",
    "lname1"
  ],
  [
    "FNAME2MNAME2LNAME2",
    "mname2",
    "lname2"
  ],
  [
    "FNAME3MNAME3LNAME3",
    "mname3",
    "lname3"
  ]
]
*/

Some points:

  1. you don't need .valueOf() here to get the value;
  2. concat don't modify the actual value, but creates a new String. So, you'll have to assign your result to StudentList[i][0];
  3. you can concatenate strings in JavaScript with + operator;

2 Comments

thanks @mrlew, can't recall why I end up with .valueOf() maybe I was just trying something different to see if it helps. So even after trying your code, I'm still getting error " Cannot set property "0.0" of undefined to "JOHNPETERSMITH"". Though atleast the uppercase and joining functions works, the issue seems to be with inserting the string back into the array.
the above code works with that structure I posted, since you didnt show your data. The table diagram is not enough. There is no such thing as column in JavaScript. You can have an Array of array (what I wrote). So you have to know your data before doing any operation. Please do a console.log(StudentList) and edit your question with the result (F12 to open console).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.