1

I've seen posts where you use a variation of .push() .push(undefined) and .push(null) but none seem to work.. my issue might be that when I am joining the list into a string, it gets rid of the empty ones.

The ultimate goal is to iterate through an array of arrays, then for each item in a "row", remove commas from the item if it is a string, and has a comma. My code is messy, I apologize.

Due to a lot of comments I shortened the code as much as I could.. I wanted to include my .join() because I think that might be part of my issue, thanks.


    let someRow = [6, "Gaston,OH", "Brumm", "Male", , , , 2554];
    function remCommas(row) {
      let newRow = [];
      for (let item in row) {
        if (typeof row[item] === "string") {
          let Str = row[item].split();
          console.log(Str);
          let newStr = [];
          for (let char in Str) {
            if (Str[char] !== ",") {
              newStr.push(Str[char]);
            }
          }
          newRow.push(newStr.join());
        } else {
          newRow.push(row[item]);
        }
      }
      console.log(newRow);
      return newRow;
    }
    remCommas(someRow);

//          input: [6, "Gaston, OH", "Brumm", "Male", , , , 2554]
//expected output: [6, "Gaston OH", "Brumm", "Male", , , , 2554]
//  current ouput: [6, "Gaston, OH", "Brumm", "Male", 2554]
12
  • 1
    what is an "empty element" for you? There is a difference between having undefined as an element, and actually having a sparse array, even if it's very subtle. Commented Feb 17, 2020 at 16:54
  • in my console.log in my browser, it shows the following: [6, "Gaston", "Brumm", "Male", empty × 3, 2554] and if you click the arrow, it shows the index skip from 3 to 7. Commented Feb 17, 2020 at 16:57
  • Please provide a minimal reproducible example, with example inputs and outputs, especially with what you're expecting to get, and what you're actually getting. Note that using for..in over an array is considered a bad practice. Commented Feb 17, 2020 at 16:57
  • it's good to put code, but it should be simplified so that it only reflects the question asked, not the equation of a trip to mars Commented Feb 17, 2020 at 16:59
  • 2
    It isn't clear your goal. for instance for the input [6, "Gaston", "Brumm", "Male", , , , 2554]; what is the expected output. Commented Feb 17, 2020 at 17:40

1 Answer 1

2

The hardest part was to understand the question, well if I understood correctly?

const input_Row = [6, "Gaston, OH", "Brumm", "Male", , , , 2554];

let output_Row = input_Row.map(row=>(typeof row==='string') ?row.replace(/,/g,'') :row)

console.log ('input_Row', input_Row)
console.log ('output_Row', output_Row)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Thank you so much. Yes, this works! I deeply apologize for the confusion and will aim to be as clear as possible in the future. I really appreciate it.
Please note this code version leaves the holes in the array thus you will have [6, "Gaston OH", "Brumm", "Male", undefined,undefined ,undefined , 2554]; because the map ignores the holes.
@Umbert Yes, I understand, this was my goal. To remove commas and to keep those undefined's in there, as I will be exporting the data to a spreadsheet and need to keep the cell data in the correct columns. Thank you for the follow up, though. (:

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.