0

So i have a script which outputs me answers in different lines like this:

 111.111
 111
 111.111
 111 

And i want that output would look like this:

111.111:111
111.111:111 

Its just putting every second line back and adding a : between them. I believe i need to change this part of my code:

$('#table, td').each(function(index,element) {
     if(index%8==1 || index%8==2){
           console.log($(this).text());
     }
});
greyb3ast
  • 79
  • 1
  • 7
  • 3
    node.js? Add the code of NodeJS, dont' use jQuery to do this. – Tushar Jan 20 '16 at 14:08
  • in node.js use: process.stdout.write() instead of console.log(), or just use the console.log() for the newline – João Henriques Jan 20 '16 at 14:11
  • @tushar i use this in my node.js code and it works fine – greyb3ast Jan 20 '16 at 14:12
  • I think you're confused about NodeJS and jQuery. Try: `var text = ''; $('#table, td').each(function(ind) { if (ind % 8 === 1) { text = $(this).text(); } else if (ind % 8 === 2) { console.log(text + ':' + $(this).text()); text = ''; }});` – Tushar Jan 20 '16 at 14:13
  • 1
    @Tushar it's not jQuery in client ... it's cheerio in node – charlietfl Jan 20 '16 at 14:13
  • Where does the regex and cheerio come from? – Universal Electricity Jan 20 '16 at 14:15
  • @charlietfl Thanks for the info. It looks like jQuery, and I didn't notice the tag. But my above code should work. – Tushar Jan 20 '16 at 14:15
  • loop over rows instead of cells then combine the text needed on each row – charlietfl Jan 20 '16 at 14:15
  • @tushar thank you bro it works like i wanted :D – greyb3ast Jan 20 '16 at 14:20
  • If you're scraping specific columns of a table, you should probably start by selecting ``s, then inside that loop pick the `` rows you want rather than going directly to ``s. `#table, td` is an odd selector, iterating tables and their cells in the same loop. But hard to say without the HTML, and I know this is an old (unanswered) post. – ggorlen Jan 01 '23 at 07:26

0 Answers0