1

From my ata i only have to fetch first 5 values. Using Jquery how it is possible.

    var server =[
    {
        "jobid": "4",
        "browser": "FF20",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:06 GMT+0530 (IST)",
        "_id": "524d269a6d32804512000001"
    },
    {
        "jobid": "34",
        "browser": "GC23",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:47 GMT+0530 (IST)",
        "_id": "524d26c36d32804512000002"
    },
    {
        "jobid": "34",
        "browser": "IE8",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:50 GMT+0530 (IST)",
        "_id": "524d26c66d32804512000003"
    },
    {
        "jobid": "34",
        "browser": "FF20",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:53 GMT+0530 (IST)",
        "_id": "524d26c96d32804512000004"
    },
    {
        "jobid": "34",
        "browser": "GC23",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:55 GMT+0530 (IST)",
        "_id": "524d26cb6d32804512000005"
    },
    {
        "jobid": "34",
        "browser": "IE8",
        "subemail": "0poo",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:41:57 GMT+0530 (IST)",
        "_id": "524d26cd6d32804512000006"
    },
    {
        "jobid": "86",
        "browser": "FF20",
        "subemail": "",
        "userid": "0",
        "names": "Guest",
        "datetime": "Thu Oct 03 2013 13:42:27 GMT+0530 (IST)",
        "_id": "524d26eb6d32804512000007"
    },
    {
        "jobid": "86",
        "browser": "GC23",
        "subemail": "",
        "userid": "0",
        "names": "Guest",
        "datetime": "Thu Oct 03 2013 13:42:31 GMT+0530 (IST)",
        "_id": "524d26ef6d32804512000008"
    },
    {
        "jobid": "86",
        "browser": "IE8",
        "subemail": "",
        "userid": "0",
        "names": "Guest",
        "datetime": "Thu Oct 03 2013 13:42:32 GMT+0530 (IST)",
        "_id": "524d26f06d32804512000009"
    },
    {
        "jobid": "86",
        "browser": "FF20",
        "subemail": "",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:43:01 GMT+0530 (IST)",
        "_id": "524d270d6d3280451200000a"
    },
    {
        "jobid": "86",
        "browser": "GC23",
        "subemail": "",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:43:03 GMT+0530 (IST)",
        "_id": "524d270f6d3280451200000b"
    },
    {
        "jobid": "86",
        "browser": "IE8",
        "subemail": "",
        "userid": "60",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:43:05 GMT+0530 (IST)",
        "_id": "524d27116d3280451200000c"
    },
    {
        "jobid": "86",
        "browser": "FF20",
        "subemail": "",
        "userid": "11",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:44:35 GMT+0530 (IST)",
        "_id": "524d276b6d3280451200000d"
    },
    {
        "jobid": "86",
        "browser": "GC23",
        "subemail": "",
        "userid": "11",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:44:37 GMT+0530 (IST)",
        "_id": "524d276d6d3280451200000e"
    },
    {
        "jobid": "86",
        "browser": "IE8",
        "subemail": "",
        "userid": "11",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:44:39 GMT+0530 (IST)",
        "_id": "524d276f6d3280451200000f"
    },
    {
        "jobid": "86",
        "browser": "FF20",
        "subemail": "",
        "userid": "11",
        "names": "[email protected]",
        "datetime": "Thu Oct 03 2013 13:45:13 GMT+0530 (IST)",
        "_id": "524d27916d32804512000010"
    }
]

-

serverResult.forEach(function (result) {
// How can i get the first 5 values from the data

});
1
  • 2
    I've edited out all references to JSON. There's no JSON in your question. Commented Oct 3, 2013 at 10:05

6 Answers 6

4

you can use the slice method

var first5 = server.slice(0,5)

array.slice(begin[, end])

Get the detailed Syntax here

Sign up to request clarification or add additional context in comments.

Comments

3

Try using this:

server.slice(0,5)

1 Comment

From (SLICE)[developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… : Zero-based index at which to end extraction. slice extracts up to but not including end. "
0

Use a for loop:

for (var i = 0; i < 5; i++) {
   var obj = server[i];

   // Do something...
   // obj.jobid, obj.browser, etc.
}

Of if you're using jQuery:

$.getJSON('/api/Controller/Action', {}, function (result) {
    $.each(result, function (i, v) {
        if (i > 4) return false; // Stop processing after 5 entries

        // 'v' now is your current object
        // v.jobid, v.browser, etc.
    });
});

Comments

0
for(j=0;j<server.length;j++){
    var elms = server[j];
    for(i=0;i<elms.length;i++){
         if(i<4){
             console.log(elms[i].jobid); //etc
          }else{
       break;
      }
    }


}

Comments

0

I think it should be .each() and not .forEach()

server.each(function (index,result) {
  if(index<5){
   //do stuff;
  //array.push(item);
  }     
});

2 Comments

showing SyntaxError: unlabeled break must be inside loop or switch
@Psl used return false; check it
0

Try this code, i guess that this is what you need,

$(document).ready(function() {
    $.each(server, function(index, item) {
                if(index < 5) {
                    //DoSomeThings
                } else {
                    return;
                }
            });
       });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.