Issue
I have a table where I fetch the values of columns 1st td and 9th tds of all rows
$("table tr").each(function() {
return ($(this).find("td").eq(0).text() + " " + $(this).find("td").eq(8).text())
}).join("#")
I need to get the result in the form of array
as
["apple 20", "banana 30", "pears 30"].join("#")
and the expected result is
apple 20#banana 30#pears 30
How can I modify my iteration to return an array. I may then join with any characters I need.
Solution
You can do it like this:
var result = $("table tr").map(function() {
return ($(this).find("td").eq(0).text() + " " + $(this).find("td").eq(1).text())
}).get().join("#").
.each will not return the redirected result you want, it will return all the tr in your table
Demo
var n = $("table tr").each(function() {
return ($(this).find("td").eq(0).text() + " " + $(this).find("td").eq(1).text())
})
console.log(n)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
Answered By - Carsten Løvbo Andersen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.