Find the 2nd td of every table and push it into array


Find the 2nd td of every table and push it into array



I have this HTML table:




var code = ;
var pname = ;
var description = ;
var size = ;
var printarea = ;
var colours = ;
var comments = ;

var cell = document.getElementsByTagName("td");


for (var i = 1; i < cell.length; i += 14) {
var c = cell[i].innerHTML;
code.push(c);
}

console.log(code);
console.log(pname);

// code = VISA01, UMBR21
// name = Sun Viscor Clip, Compact Umbrella With Steel Shaft
// description = Stylish Sun Visor Clip which holds your sunglasses in place and within reachProtects your sunglasses away from being scratchedEasy to use, sturdy, clips well and does not block your vision while driving SIZE 8 (w) x 3 (h) cm, Compact umbrella with steel shaft for a strong wind-resistant performancePush button and auto-open for a convenient and effortless openingDurable Logo printed on front and back panels; comes in several colors; can combine colors upon request


<html>

<head>
<title>Table to Array</title>


<body>
<table class="ty-table ty-qty-discount_ _table">
<tbody>
<tr>
<td>CODE</td>
<td>VISA01</td>
</tr>
<tr>
<td>NAME</td>
<td>Sun Visor Clip</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>Stylish Sun Visor Clip which holds your sunglasses in place and within reachProtects your sunglasses away from being scratchedEasy to use, sturdy, clips well and does not block your vision while driving</td>
</tr>
<tr>
<td>SIZE</td>
<td>8 (w) x 3 (h) cm</td>
</tr>
<tr>
<td>PRINT AREA</td>
<td>4 (w) x 1 (h) cm</td>
</tr>
<tr>
<td>COLOURS</td>
<td>navy, black, red, royal, white, orange, green</td>
</tr>
<tr>
<td>COMMENTS</td>
<td></td>
</tr>
</tbody>
</table>

<table class="ty-table ty-qty-discount__table">
<tbody>
<tr>
<td>CODE</td>
<td>UMBR21</td>
</tr>
<tr>
<td>NAME</td>
<td>Compact Umbrella With Steel Shaft</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>Compact umbrella with steel shaft for a strong wind-resistant performancePush button and auto-open for a convenient and effortless openingDurable Logo printed on front and back panels; comes in several colors; can combine colors upon request
</td>
</tr>
<tr>
<td>SIZE</td>
<td>53 (h) cm</td>
</tr>
<tr>
<td>PRINT AREA</td>
<td>20 (w) x 20 (h) cm</td>
</tr>
<tr>
<td>COLOURS</td>
<td>black, white, red, blue.please call for more colour options</td>
</tr>
<tr>
<td>COMMENTS</td>
<td></td>
</tr>
</tbody>
</table>
</body>

</html>



I want to extract the values from every second <td> and put it in to an array.


<td>



For example:



code array contains VISA01, UMBR21
name array contains Sun Viscor Clip, Compact Umbrella With Steel Shaft


code


VISA01


UMBR21


name


Sun Viscor Clip


Compact Umbrella With Steel Shaft



Can anyone please help me out?





Please read the following two links: stackoverflow.com/help/mcve and stackoverflow.com/help/how-to-ask
– aydow
Jun 29 at 3:23






Possible duplicate of How to select nth element of the same type
– Rehan Haider
Jun 29 at 11:31




3 Answers
3



You can use reduce to group the selection and ES6 destructuring assignment to access the required vars:


reduce


const {
code,
name: _name,
description,
size,
printarea,
colours,
comments
} = [...document.querySelectorAll('.ty-table')].reduce((all, table) => {

[...table.getElementsByTagName('tr')].forEach(tr => {

const [key, val] = [...tr.getElementsByTagName('td')].map(n => n.innerText.trim());

const keyLc = key.replace(/ /g,'').toLowerCase();

if (!all.hasOwnProperty(keyLc)) {
all[keyLc] = ;
}

all[keyLc].push(val);

})

return all;

}, {});;

console.log(code);
console.log(_name);
console.log(description);
console.log(size);
console.log(printarea);
console.log(colours);
console.log(comments);



Note that we have _name alias for name, as name is a reserved JS keyword and accessors with name convert all data structures to string:


Note


_name


name


name


name


var name = [1,2,3];
typeof name === 'string' // true



General practice is not to use name as your variable name.


name



Give it a try




<html>
<head>
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js

$(document).ready(function(){
$("table tr > td:nth-child(2)").each(function(index, elem){
console.log(elem);
});
});

</head>


<body>
<table class="ty-table ty-qty-discount_ _table">
<tbody>
<tr>
<td>CODE</td>
<td>VISA01</td>
</tr>
<tr>
<td>NAME</td>
<td>Sun Visor Clip</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>Stylish Sun Visor Clip which holds your sunglasses in place and within reachProtects your sunglasses away from being scratchedEasy to use, sturdy, clips well and does not block your vision while driving</td>
</tr>
<tr>
<td>SIZE</td>
<td>8 (w) x 3 (h) cm</td>
</tr>
<tr>
<td>PRINT AREA</td>
<td>4 (w) x 1 (h) cm</td>
</tr>
<tr>
<td>COLOURS</td>
<td>navy, black, red, royal, white, orange, green</td>
</tr>
<tr>
<td>COMMENTS</td>
<td></td>
</tr>
</tbody>
</table>

<table class="ty-table ty-qty-discount__table">
<tbody>
<tr>
<td>CODE</td>
<td>UMBR21</td>
</tr>
<tr>
<td>NAME</td>
<td>Compact Umbrella With Steel Shaft</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>Compact umbrella with steel shaft for a strong wind-resistant performancePush button and auto-open for a convenient and effortless openingDurable Logo printed on front and back panels; comes in several colors; can combine colors upon request
</td>
</tr>
<tr>
<td>SIZE</td>
<td>53 (h) cm</td>
</tr>
<tr>
<td>PRINT AREA</td>
<td>20 (w) x 20 (h) cm</td>
</tr>
<tr>
<td>COLOURS</td>
<td>black, white, red, blue.please call for more colour options</td>
</tr>
<tr>
<td>COMMENTS</td>
<td></td>
</tr>
</tbody>
</table>
</body>

</html>



I solved it by doing this.. not the best/elegant solution but at least it works.


var code = ;
var pname = ;
var description = ;
var size = ;
var printarea = ;
var colours = ;
var comments = ;

var cell = document.getElementsByTagName("td");


for (var i = 1; i < cell.length; i+=14) {
var c = cell[i].innerHTML;
code.push(c);
}

for (var i = 3; i < cell.length; i+=14) {
var n = cell[i].innerHTML;
pname.push(n);
}

for (var i = 5; i < cell.length; i+=14) {
var d = cell[i].innerHTML;
description.push(d);
}

for (var i = 7; i < cell.length; i+=14) {
var s = cell[i].innerHTML;
size.push(s);
}

for (var i = 9; i < cell.length; i+=14) {
var p = cell[i].innerHTML;
printarea.push(p);
}

for (var i = 11; i < cell.length; i+=14) {
var col = cell[i].innerHTML;
colours.push(col);
}

for (var i = 13; i < cell.length; i+=14) {
var com = cell[i].innerHTML;
comments.push(com);
}

console.log(code);
console.log(pname);
console.log(description);
console.log(size);
console.log(printarea);
console.log(colours);
console.log(comments);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV