How can I extract and save the text from a span element as key value pairs within a div?
<div class="data-diff-span__composite-list-item__18c5zip data-diff-core__highlight-area__19c0zip">
<div class="data-diff-basic__class-row__4ngp30a">
<div class="data-diff-basic__class-header__4ngp30a">
<span class="data-diff-basic__class-name__4ngp30a">ADDITIONAL_WRK</span>
<span class="sp3-tag sp3-intent-danger">
<span class="sp3-text-overflow-ellipsis sp3-fill">DECIMAL</span>
</span>
</div>
</div>
</div>
<div class="data-diff-span__composite-list-item__18c5zip data-diff-core__highlight-area__19c0zip">
<div class="data-diff-basic__class-row__4ngp30a">
<div class="data-diff-basic__class-header__4ngp30a">
<span class="data-diff-basic__class-name__4ngp30a"> AFFECTMRAPPLIC</span>
<span class="sp3-tag sp3-intent-danger">
<span class="sp3-text-overflow-ellipsis sp3-fill"> DECIMAL</span>
</span>
</div>
</div>
</div>
<div class="data-diff-span__composite-list-item__18c5zip data-diff-core__highlight-area__19c0zip">
<div class="data-diff-basic__class-row__4ngp30a">
<div class="data-diff-basic__class-header__4ngp30a">
<span class="data-diff-basic__class-name__4ngp30a">APPROVED_ON</span>
<span class="sp3-tag sp3-intent-danger">
<span class="sp3-text-overflow-ellipsis sp3-fill">TIMESTAMP</span>
</span>
</div>
</div>
</div>
<div class="data-diff-span__composite-list-item__18c5zip data-diff-core__highlight-area__19c0zip">
<div class="data-diff-basic__class-row__4ngp30a">
<div class="data-diff-basic__class-header__4ngp30a">
<span class="data-diff-basic__class-name__4ngp30a">COMPOSITE</span>
</div>
</div>
</div>
To individually access them, you can use the following:
// First span
var firstSpan= document.getElementsByClassName( 'data-diff-basic__class-name__4ngp30a' )
var firstSpanArray = [];
for ( var i = 0; i < firstSpan.length; ++i ) {
firstSpanArray.push( firstSpan[i].innerText );
}
// Second span
var secondSpan= document.getElementsByClassName( 'sp3-text-overflow-ellipsis sp3-fill' )
var secondSpanArray = [];
for ( var i = 0; i < secondSpan.length; ++i ) {
secondSpanArray.push( secondSpan[i].innerText);
}
Now, I want to combine firstSpanArray
and secondSpanArray
using their index into a key value JavaScript object. However, some spans may not have a corresponding second span, leading to an index mismatch.
Can anyone help me modify this query to achieve the following result:
spanCollection = {
"ADDITIONAL_WRK":"DECIMAL",
"AFFECTMRAPPLIC":"DECIMAL",
"APPROVED_ON":"TIMESTAMP",
"COMPOSITE":""
}