0

I have numerous tsv files each with header row. Now one column name in header row is age. In few files, column name is age while in other files it has EOL charcter such as \r \n.

Now how can i use str.indexOf('age') function so that i get index of age irrespective of column name age with EOL character such as \n , \r etc..

Foe eg: tsv file1:

Name    Address   Age    Ph_Number

file 2:

Name    Address   Age/r

file 3:

Name    Address   Age\n

I am trying to find index of age column in each files header row.
However when i do-

header.indexOf('age')

it gives me result only in case of file1 because in other 2 files we have age as age\r and age\n..

My question is how should i find index of age irrespective of \r \n character along with age in header row.

i have following script now:

var headers = rows[0].split('\t');
    if (file.name === 'subjects.tsv'){
      for (var i = 0; i < rows.length; i++) {
        var ageIdColumn = headers.indexOf("age");
        console.log(headers)
6
  • Not sure what you are asking. In the case of "name age address" You want to get 1? or 5? Commented Feb 20, 2017 at 22:36
  • 2
    Does indexOf not work? Being followed by \n or \r shouldn't affect the result of indexOf. Commented Feb 20, 2017 at 22:38
  • 7
    indexOf() returns the index of where the input string starts in the searched string. It will return the same number for age whether it is followed by other characters or not. Commented Feb 20, 2017 at 22:39
  • In addition to previous comments re indexOf, you could also pre-process the CSV files to modify line-endings with a separate program/script/automated process if you really need to normalize those for other reasons. Commented Feb 20, 2017 at 22:41
  • Are you really saying you have a tsv file and not a csv file? What is that? Commented Feb 20, 2017 at 22:54

1 Answer 1

1

As I stated in the comments, indexOf() returns the starting position of the string. It doesn't matter what comes after it:

var csvFile1 = 'column1,column2,column3,age,c1r1';
var csvFile2 = 'column1,column2,column3,age\r,c1r1';
var csvFile3 = 'column1,column2,column3,age\n,c1r1';

console.log(csvFile1.indexOf("age"));
console.log(csvFile2.indexOf("age"));
console.log(csvFile3.indexOf("age"));

If you specifically want to find the versions with the special characters, just look for them explicitly:

var csvFile4 = 'column1,age\r,column2,column3,age\n,c1r1';
console.log(csvFile4.indexOf("age\r"));
console.log(csvFile4.indexOf("age\n"));

Lastly, it may be that you are confused as to what, exactly indexOf() is supposed to do. It is not supposed to tell you where all occurrences of a given string are. It stops looking after the first match. To get all the locations, you'd need a loop similar to this:

var csvFile5 = 'column1,age\r,column2,age, column3,age\n,c1r1';

var results = []; // Found indexes will be stored here.
var pos = null;   // Stores the last index position where "age" was found

while (pos !== -1){
  // store the index where "age" is found 
  // If pos is not null, then we've already found age earlier and we 
  // need to start looking for the next occurence 3 characters after
  // where we found it last time. If pos is null, we haven't found it
  // yet and need to start from the beginning.
  pos = csvFile5.indexOf("age", pos != null ? pos + 3 : pos );
  pos !== -1 ? results.push(pos) : "";
}

// All the positions where "age" was in the string (irrespective of what follows it)
// are recorded in the array:
console.log(results);

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

2 Comments

Perfect.. But if i have headers in an array and want to find headers.indexOf('age'), then its not working..
i need to reframe my question. i didnt share complete problem. new to javascript.

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.