extract all textcontent in htmlcollection to array with javascript Here is the raw data
anycasestr = &quot;&lt;div style=&quot;color: rgb(51, 51, 51); background-color: rgb(253, 246, 227); font-family: Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-size: 12px; line-height: 18px;&quot;&gt;&lt;div&gt;refinement&lt;/div&gt;&lt;div&gt;decent&lt;/div&gt;&lt;div&gt;elegant&lt;/div&gt;&lt;/div&gt;&quot;;

to extract all the textcontent to an array like [&quot;refinement&quot;, &quot;decent&quot;, &quot;elegant&quot;]
I wrote this code (jquery has been included)
htmlObject = $(anycasestr);
arr = Array.prototype.slice.call(htmlObject[0].getElementsByTagName(&quot;div&quot;));
arr_page = [];
for (i = 0; i &lt; 3; i++) {
    arr_page.push(arr[i].textContent);
}

It produces what I want.
I'm just concerned if it is implemented in an elegant way. Could someone take a look at it?
For example, should I have used xpath or something, in terms of computational concerns?
Declare your variables - whenever you assign to or reference a variable without defining it first, you will either (1) implicitly create a property on the global object (which can result in weird bugs), or (2) throw an error, if you're running in strict mode. You currently aren't defining any of your variables. Fix it by putting const (or, when needed, let) in front of them when assigning to them for the first time, eg const htmlObject = $(anycasestr);.
jQuery or DOM methods? You're using jQuery to turn the string into a jQuery collection of elements, but then you're using getElementsByTagName to select children. If you're using jQuery, you can be concise and consistent to use it to select the &lt;div&gt; children.. To find children of an element which match a particular tag name, call .find on the jQuery collection - then, you can use .map to turn the found jQuery elements into a collection of just the text of the elements:
const $parent = $(anycasestr);
const arr = $parent.find('div')
  .map((_, child) =&gt; child.textContent)
  .get(); // turn the jQuery collection of strings into an array of strings



const anycasestr = `&lt;div style="color: rgb(51, 51, 51); background-color: rgb(253, 246, 227); font-family: Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-size: 12px; line-height: 18px;"&gt;&lt;div&gt;refinement&lt;/div&gt;&lt;div&gt;decent&lt;/div&gt;&lt;div&gt;elegant&lt;/div&gt;&lt;/div&gt;`;
const $parent = $(anycasestr);
const arr = $parent.find('div')
  .map((_, child) =&gt; child.textContent)
  .get(); // turn the jQuery collection of strings into an array of strings
console.log(arr);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;



Or, you can use DOMParser instead. Using DOMParser rather than jQuery to turn the text into a collection of elements can avoid accidental execution of malicious scripts. Example exploit using jQuery:


const anycasestr = `&lt;img src="" onerror="alert('evil')"&gt;`;
const $parent = $(anycasestr);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;



With DOMParser:


const anycasestr = `&lt;div style="color: rgb(51, 51, 51); background-color: rgb(253, 246, 227); font-family: Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-size: 12px; line-height: 18px;"&gt;&lt;div&gt;refinement&lt;/div&gt;&lt;div&gt;decent&lt;/div&gt;&lt;div&gt;elegant&lt;/div&gt;&lt;/div&gt;`;
const doc = new DOMParser().parseFromString(anycasestr, 'text/html');
const arr = [...doc.querySelectorAll('div &gt; div')]
  .map(div =&gt; div.textContent);
console.log(arr);



The query string div &gt; div selects &lt;div&gt; elements which are direct children of another &lt;div&gt;. It works exactly the same way as CSS selectors. querySelectorAll is a great tool for concise selection of elements - it can be easier to write and understand at a glance than other methods (like your original htmlObject[0].getElementsByTagName(&quot;div&quot;)).
Array.prototype.slice.call is a bit verbose - on non-ancient environments, you can use spread syntax instead, like I did above. Creating an array all at once by mapping is also somewhat more elegant than declaring an array then .pushing onto it.
If you had more &lt;div&gt; children and wanted to take only the text from the first 3 of them, it'd be more functional to .slice the array of elements instead of putting an iteration count in a for loop:


const anycasestr = `&lt;div style="color: rgb(51, 51, 51); background-color: rgb(253, 246, 227); font-family: Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-size: 12px; line-height: 18px;"&gt;
  &lt;div&gt;refinement&lt;/div&gt;
  &lt;div&gt;decent&lt;/div&gt;
  &lt;div&gt;elegant&lt;/div&gt;
  &lt;div&gt;don't include me&lt;/div&gt;
  &lt;div&gt;don't include me&lt;/div&gt;
  &lt;div&gt;don't include me&lt;/div&gt;
&lt;/div&gt;`;
const doc = new DOMParser().parseFromString(anycasestr, 'text/html');
const arr = [...doc.querySelectorAll('div &gt; div')]
  .slice(0, 3)
  .map(div =&gt; div.textContent);
console.log(arr);




in terms of computational concerns?

Unless the stuff that needs to be parsed is unreasonably large, performance for this sort of thing is not a concern; better to write clean, readable, maintainable code. If you later find that something is taking longer to run than is ideal, you can identify the bottleneck and then figure out how to fix it. (But this almost certainly won't be the bottleneck.)