0

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.

1 Answer 1

1

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>

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

Comments

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.