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]
Brandons404
  • 109
  • 1
  • 9
  • 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. – ASDFGerte Feb 17 '20 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. – Brandons404 Feb 17 '20 at 16:57
  • Please provide a [mre], 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](https://stackoverflow.com/q/500504/215552). – Heretic Monkey Feb 17 '20 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 – Mister Jojo Feb 17 '20 at 16:59
  • @MisterJojo I appologize.. I tried to simplify it more than before, I hope it is bearable to read now (: – Brandons404 Feb 17 '20 at 17:26
  • 2
    It isn't clear your goal. for instance for the input [6, "Gaston", "Brumm", "Male", , , , 2554]; what is the expected output. – Nick Feb 17 '20 at 17:40
  • @Umbert Sorry for not making my question clear, first time posting. I cleaned it up a bit with input/output examples at the bottom. – Brandons404 Feb 17 '20 at 18:02
  • help me to understand, the goal is remove the `,` from each string element in the row? – Quethzel Diaz Feb 17 '20 at 18:17
  • I would recommend using `replace`. Str.replace(',' , ' ') does what I think you're trying to accomplish. For instance `"Gaston, OH".replace(',', ' ')` === `"Gaston OH"`. That would simplify your code further. – Angrysheep Feb 17 '20 at 18:18
  • @Angrysheep I actually just found this while looking around, and I am trying to implement it now! – Brandons404 Feb 17 '20 at 18:22
  • @QuethzelDíaz My main issue is my array gets shortened after I use .join(). It will not keep the "empty" elements or placeholders in my array. Sorry for any confusion. – Brandons404 Feb 17 '20 at 18:24
  • @Brandons404 Good luck. Let us know if you're still having trouble! – Angrysheep Feb 17 '20 at 18:24

1 Answers1

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; }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • 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. – Brandons404 Feb 17 '20 at 18:30
  • 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. – Nick Feb 17 '20 at 18:36
  • 1
    @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. (: – Brandons404 Feb 17 '20 at 18:38