Please help optimize and write Jquery function that gets json data and appends it to select list options
<p>Basically just need to write a jQuery/Ajax that fetches Json data (Price data) from server
and appends/overwrites each options text value so it would have the price difference between the
selected option and non selected option on the end of it. Only the non selected option should have the price difference showing on the end of it, see example below.</p>

<p>The code you will find below pretty much does this, but I can't seem to properly append/overwrite
it to the end of the option text value without the price difference being repeated (not replaced) onto the end with every onchange of the dropdown list. So I get [product name025252525] etc.</p>

<p>As well no idea how to not append the difference to the selected options text, I just get "0" there now as it minuses itself from itself.</p>

<p>The Json object (data) array is of the format <code>{partid = 3, price = 234}, {partid = 6, price = 53}</code> etc.</p>

<p>List should look like so:</p>

<pre><code>[Intel i7 950] - selected visible option
[Intel i7 960 (+ $85)] - not selected but in the drop down list
[Intel i7 930 (- $55)] - not selected but in the drop down list



&lt;script type="text/javascript"&gt;



    $(document).ready(function () {
        var arr = new Array();
        $('select option').each(function () {
            arr.push($(this).val());
        });






        $.ajax({
            type: "POST",
            url: "/Customise/GetPartPrice",
            data: { arr: arr },
            traditional: true,
            success: function (data) { mydata = data;  OnSuccess(data) },
            dataType: "json"



        });




    });





   $('select').change(function () { OnSuccess(mydata); });


    function OnSuccess(data) {



        $('select').each(function () {


            var sov = parseInt($(this).find('option:selected').attr('value')) || 0; //Selected option value

            var sop; //Selected Option Price


            for (i = 0; i &lt;= data.length; i++) {


                if (data[i].partid == sov) {

                    sop = data[i].price;
                    break;
                }


            };









            $(this).find('option').each(function () {

                // $(this).append('&lt;span&gt;&lt;/span&gt;');

                var uov = parseInt($(this).attr('value')) || 0; //Unselected option value

                var uop; //Unselected Option Price


                for (d = 0; d &lt;= data.length; d++) {


                    if (data[d].partid == uov) {

                        uop = data[d].price;
                        break;
                    }

                }

                var newtext = uop - sop;



                    var xtext = $(this).text().toString();
                    $(this).attr("text", xtext + newtext);


                // mob.append(newtext)
                // $(this).next('span').html(newtext);


            });







        });


    };




   //$(document).ready(function () { $("#partIdAndCount_0__PartID").prepend('&lt;option value="0"&gt;Select Processor&lt;option&gt;'); });


&lt;/script&gt;
</code></pre>
