Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing first element in js collection

in my js function I had following

success: function (result) {
     $.each(result, function (i, item) {                  
        $("#tab-" + item.myType).append(result);
     })
}

Now I've change this function and instead I have following

success: function (result) {
   $("#tab-how To Access to the first result collection element ").html(result);
 }
}

I tried with

$("#tab-" + result[0].item.myType).html(result);

but it didn't help.

Question is: How to access to the first js element inside result collection object

Any thoughts ?

Using console.log(result) I'm getting html snippets which I display on partial view

div class="product clearfix">
        <div class="l-new">
            <a href="#">
                @Model.MyType (rendered as My Type)
            </a> 
....
</div>
like image 712
user1765862 Avatar asked Oct 26 '25 13:10

user1765862


1 Answers

You can try:

result[0] // if you have numeric indices in your result collection

OR

var firstItem = null;
for(var x in result) { // if you have named properties
    firstItem = result[x];
    break;
}
like image 182
techfoobar Avatar answered Oct 29 '25 03:10

techfoobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!