locked
retrieve string using jquery RRS feed

  • Question

  • User932259438 posted

    Hi,

    I have string:
    amount:12 id:139410549,stake:13 amount:13978357 id:13978357

    How can i find amount 13 if i have id: 13978357, or amount=12 if I have 139410549?

    Thank you.

    Saturday, December 10, 2016 1:12 PM

Answers

  • User475983607 posted

    progy85

    Hi,

    I have string:
    amount:12 id:139410549,stake:13 amount:13978357 id:13978357

    How can i find amount 13 if i have id: 13978357, or amount=12 if I have 139410549?

    Thank you.

    Please post the relevant bits of your code.  You're showing fragments that do not make sense in the given context.

    I assume the JSON object (not JQuery) looks similar to this?

    var data = [{amount:12, id:139410549, stake:13}, 
    {id:13978357, amount:13978357, stake:0}]

    Here data is an array of object.  This retrieves the object at index zero.

    data[0]

    To find a specific ID in the data collection, you'll need to iterate over the data collection to find the target ID. The example below is using JQuery's $.each() function.

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
            $(function () {
    
                //Data
                var data = [{ amount: 12, id: 139410549, stake: 13 },
                       { id: 13978357, amount: 13978357, stake: 0 }];
    
                $('#Button1').click(function () {
                    //Get the search id from the text input
                    var id = $('#search').val();
    
                    //Find the id in the collection and write the result to the console
                    //and to the DOM
                    $.each(data, function (index, value) {
                        if (value.id == id) {
                            console.log(value);
                            $('#result').html(JSON.stringify(value));
                        }
                    });
                    
                });
    
            });
        </script>
    </head>
    <body>
        <input id="Button1" type="button" value="button" />
        <input id="search" type="text" value="139410549">
        <div id="result"></div>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 10, 2016 1:31 PM