简体   繁体   中英

Filter Key value of json on ajax success

Is there a way to filter my key value.I want to filter key where my key is REF, and my value is text1. Get the result where a key is REF and my value is text1 . I want to access each key value separate when the result is returned. Like alert(SNO, value); where SNO is my key and the Value that is returned from my filter result

<script type="text/javascript">
       function ClientNodeClicking(sender, eventArgs) {
           var node = eventArgs.get_node();
           var text1 = node.get_text();
           var text2 = node.get_value();

           $.ajax({
               type: "POST",
               url: "A2_JVV.aspx/ds2json",
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (msg) {
                   $("#Result").text(msg.d);
                   $.each(msg, function (key, value) {
                       console.log(key, value);
                   });
               }

           });
           alert(text1);

       }   

   </script>

this is what i get in my chrome console

d [
  {
    "REF": "JVV-001441",
    "SNO": 1.0,
    "mdate": "2015-08-15T00:00:00",
    "date": "15/08/2015",
    "INITIAL": "G",
    "ACNO": "02010219",
    "TITLE": "S S ENT. ON LINE A/C",
    "JOB_ID": "00000",
    "JOB": "0-N/A",
    "cRR": 0.00,
    "dRR": 500000.00,
    "U_INSERT": "KHURRAM",
    "I_DATE": "2015-08-15T14:09:19.997",
    "U_EDIT": null,
    "E_DATE": null,
    "DISC": "RECEIVED ON LINE SLIP NO#5794 MEEZAN S S A/C (AMJAD ANJUM)"
  },
  {
    "REF": "JVV-001441",
    "SNO": 2.0,
    "mdate": "2015-08-15T00:00:00",
    "date": "15/08/2015",
    "INITIAL": "C",
    "ACNO": "020101039",
    "TITLE": "AMJAD ANJUM",
    "JOB_ID": "00000",
    "JOB": "0-N/A",
    "cRR": 500000.00,
    "dRR": 0.00,
    "U_INSERT": "KHURRAM",
    "I_DATE": "2015-08-15T14:09:20.01",
    "U_EDIT": null,
    "E_DATE": null,
    "DISC": "RECEIVED ON LINE SLIP NO#5794 MEEZAN S S A/C"
  }
]

If you want to filter any array you can use the core .filter() method of every array.

It builds a new array by iterating over every element. It will call the provided callback passing it the element in the array and if the callback returns true that element will be put into the new array, otherwise it will be ignored.

So something like this would filter out all elements in your array where the property REF is not equal to text1 ;

var x = d.filter(function (item) {
    return item.REF === 'text1';
});

You last requirement sounds like you want to iterate over each objects properties rather than access them directly. This can be done with a for in loop however the order the properties are iterated in cannot be guaranteed.

Something like this:

x.forEach(function (item){
   //forEach iterates over every object in array
    for(var i in item) {
        //for in iterates over every property in item
        console.log(i, item[i]);   
    }
});

Also because most core array methods, filter , forEach , map return the new array they can be chained.

EDIT: I lie, forEach does not return it self so cannot be chained afterwards. the others mentioned do however.

so:

var x = [].filter().map();

instead of

var x = [].filter();

x = x.map();

See fiddle: http://jsfiddle.net/cgzbvpoe/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM