1

This is my first question here, so please be nice :)

I have an array with different names in it. This looks like that: ["student:Monica", "student:Rebecca", "student:James", "teacher:Mr. Anders"]. In reality there are more names in there. From this, I want to make a list with only the students and it shouldn't say student: in front of it.

So I tried to do it like that:

var students_and_teachers = ["student:Monica", "student:Rebecca", "student:James", "teacher:Mr. Anders"];

for (i = 0; i < students_and_teachers.length; i++) {
  var a_student_or_teacher = students_and_teachers[i];
  if (a_student_or_teacher.startsWith("student:") === true) {
    students_and_teachers[i] = a_student_or_teacher.substring(8, 100);
  } else {
    delete students_and_teachers[i];
  }
}

And this works good but now when I show the students and teachers afterwards it looks like that: Array [ "Monica", "Rebecca", "James", , ] . But it should only have the three students and not the empty things in the end.

Am I doing something wrong?

Chloe_C
  • 11
  • 3
  • Oh. OK. I'm sorry :(( – Chloe_C Feb 19 '15 at 14:50
  • 1
    @Chloe_C No worries, if your question gets closed as duplicate this is fine. Actually, your question is quite good for a first-question-on-SO as you have a clear problem description and show your actual code. Keep this up and you are fine. Searching for this duplicate is not an easy task for a beginner, but will get easier over time - So seeing that you are 16 I'd like to encourage you to learn programming. – dirkk Feb 19 '15 at 15:18

3 Answers3

0

How about that ?

students_and_teachers.map(function( name ) {
  name = name.split( ':' );
  return name[ 0 ] === 'student' ? name[ 1 ] : '';
}).filter( Boolean );

Reference: Array.prototype.filter, Array.prototype.map, String.prototype.split

jAndy
  • 231,737
  • 57
  • 305
  • 359
0

Filter for students and then remove ":" try

students_and_teachers.filter(function( name ) {
  return name.indexOf("student") > -1;
}).map(function( name ) {
  return name.split( ':' )[ 1 ];
});

Result will be something like

 Array [ "Monica", "Rebecca", "James" ]
A.B
  • 20,110
  • 3
  • 37
  • 71
0

There are great alternatives posted, but the reason your code doesn't work is because delete doesn't remove an array element – it simply leaves it as undefined.

You can remove array elements by using splice.

Once you've removed the element, you need to decrement the loop variable (i--). Otherwise, it will skip over the next element.

My snippet uses indexOf , because most browsers don't yet support startsWith

var students_and_teachers = [
  "student:Monica", 
  "student:Rebecca", 
  "student:James", 
  "teacher:Mr. Anders",
  "teacher:Ms. Jones",
  "student:Rick"
];

for (i = 0; i < students_and_teachers.length; i++) {
  var a_student_or_teacher = students_and_teachers[i];
  if (a_student_or_teacher.indexOf("student:") === 0) {
    students_and_teachers[i] = a_student_or_teacher.substring(8, 100);
  } else {
    students_and_teachers.splice(i,1);
    i--;
  }
}

alert(students_and_teachers);
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79