0

Here I am Taking a string data and converting it into array elements but it is getting an empty array at last of all array elements and I am not be able to remove them easily.Please help.

let string_Data = `01226,Grover Cleveland,Anna,Uganda,Crucial Ltd,Tested Mutual B.V,Calvin Coolidge,
    77110,John F. Kennedy,hora,Bosnia Herzegovina,Formal,Papal Corporation,Franklin Roosevelt,
    29552,Lyndon B. Johnson,Margaret,Palau,Summaries Holdings Inc,Customize,Rutherford B. Hayes,`;

let making_Array = csv => {
  var data = [];

  for (let dataAry of csv.split('\n')) {

    data.push(dataAry.split(','));
  }
  return data;

}
console.log(making_Array(string_Data));

3
  • 1
    you have , at the end of each line, so there's an empty string after the last item. Commented Nov 15, 2022 at 17:57
  • Why your data has , at the end of each line? It seems like there is a column missing Commented Nov 15, 2022 at 17:57
  • since it is csv data and it is separated by comma's Commented Nov 15, 2022 at 18:14

3 Answers 3

1

Instead of manipulating arrays, you can prepare the string before being split.

let string_Data = `01226,Grover Cleveland,Anna,Uganda,Crucial Ltd,Tested Mutual B.V,Calvin Coolidge,
    77110,John F. Kennedy,hora,Bosnia Herzegovina,Formal,Papal Corporation,Franklin Roosevelt,
    29552,Lyndon B. Johnson,Margaret,Palau,Summaries Holdings Inc,Customize,Rutherford B. Hayes,`;

let making_Array = csv => {
  var data = [];

  for (let dataAry of csv.split('\n')) {
    // 1. trim both ends of the line for white space, tab, etc.
    // 2. remove any last trailing comma
    // 3. split using comma separator
    data.push(dataAry.trim().replace(/,$/, '').split(','));
  }
  return data;

}
console.log(making_Array(string_Data));


FWIW the entire function can be streamlined, and enhanced like this:

let header = ['id', 'col1', 'col2', 'col3', 'col4', 'col5', 'col6'];

let string_Data = `01226,Grover Cleveland,Anna,Uganda,Crucial Ltd,Tested Mutual B.V,Calvin Coolidge,
    77110,John F. Kennedy,hora,Bosnia Herzegovina,Formal,Papal Corporation,Franklin Roosevelt,
    29552,Lyndon B. Johnson,Margaret,Palau,Summaries Holdings Inc,Customize,Rutherford B. Hayes,`;

let making_Array = csv => csv
  .split('\n')
  .map(line => line
    .trim()             // 1. trim both ends of the line        
    .replace(/,$/, '')  // 2. remove any last trailing comma
    .split(','))        // 3. split using comma separator
    .map(cols => Object.fromEntries(new Map(header
      .map((colName, index) => [colName, cols[index] || '']))));

console.log(making_Array(string_Data));

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

3 Comments

Thanks a lot. It is of great help I tried replace too but didn't know where to use it.Thank you
No problem! If you want to convert the array to some object, take a look at the edit I just posted. Cheers!
how would you know , its my second step of the program. U r awsome
1

This will remove empty strings, but it'll also remove falsey values.

const values = arr.filter(Boolean);

2 Comments

Probably not a good idea for a CSV, as it will remove empty values in the middle of the line. That will cause different rows to have different columns.
that's why we are separating it with \n at first
0

The empty string is because of the , at the end of each line.

You can use the slice() method to remove the last element of the array.

data.push(dataAry.split(',').slice(0, -1))

When you use a negative index as the end of a slice, it counts from the end.

1 Comment

For reference, the -1 parameter counts backwards: Negative index counts back from the end of the array — if end < 0, end + array.length is used So in this case, the last element of the array is removed.

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.