Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have JSON array with subarrays and I want to loop it and find if username of user is for example 'admin'. If so then create JSON array contains data belonging to user 'admin' (region, sport, city etc). I don't have idea how to find it in loop and then slice it. I'm sorry for stupid question but I'm a little lost.

This is JSON array with structure what I have:

[
    {
        "_id": "5520f52e2c0a22541541bde1",
        "region": {
            "_id": "551e6779d8f1afa01bd86529",
            "name": "region_name"
        },
        "user": {
            "_id": "551a938af056a7fc099879c1",
            "firstName": "John",
            "lastName": "Boo",
            "username": "admin",
            "id": "551a938af056a7fc099879c1"
        },
        "__v": 0,
        "sport": [
            {
                "_id": "551e69c6d8f1afa01bd86533",
                "name": "Running"
            }
        ],
        "city": "some_city",
        "advert": "some_advert",
        "title": "I want to run!",
        "created": "2015-04-05T08:41:18.173Z"
    },
    {
        "_id": "552010740628cab002b3a700",
        "region": {
            "_id": "551e67b6d8f1afa01bd8652f",
            "name": "region_name"
        },
        "user": {
            "_id": "551a938af056a7fc099879c1",
            "firstName": "Bill",
            "lastName": "Foo",
            "username": "bill_foo",
            "id": "551a938af056a7fc099879c1"
        },
        "__v": 0,
        "sport": [
            {
                "_id": "551e5e01abb74a8423410b88",
                "nazev": "Hockey"
            }
        ],
        "city": "some_city",
        "advert": "some_advert",
        "title": "some_title",
        "created": "2015-04-04T16:25:24.733Z"
    }
]

Edit: the expected result of user 'admin' is then:

[
    {
        "_id": "5520f52e2c0a22541541bde1",
        "region": {
            "_id": "551e6779d8f1afa01bd86529",
            "name": "region_name"
        },
        "user": {
            "_id": "551a938af056a7fc099879c1",
            "firstName": "John",
            "lastName": "Boo",
            "username": "admin",
            "id": "551a938af056a7fc099879c1"
        },
        "__v": 0,
        "sport": [
            {
                "_id": "551e69c6d8f1afa01bd86533",
                "name": "Running"
            }
        ],
        "city": "some_city",
        "advert": "some_advert",
        "title": "I want to run!",
        "created": "2015-04-05T08:41:18.173Z"
}]
share|improve this question
    
Please include the expected output. – thefourtheye Apr 5 '15 at 9:50
    
show some code you've tried – Amit Joki Apr 5 '15 at 9:51
    
use jquery .each etc : [jquery loop on Json data using $.each][1] [1]: stackoverflow.com/questions/2342371/… – khodadadi Apr 5 '15 at 10:00
    
If you have your data really as JSON then you need to use JSON.parse first because [...]JSON is a 100% textual data interchange format originally inspired by JavaScript objects[...], but not a data-structure that allows you to directly access any elements. Otherwise you can use a for loop like in the answer of Matt Ellen. – t.niese Apr 5 '15 at 10:10
up vote 4 down vote accepted

Loop through the array and pull out each item with a user with a username of admin:

var result = [];
var nameToSearchFor = 'admin';

for(var index = 0; index < arr.length; index++)
{
    var item = arr[index];
    if(item.user.username === nameToSearchFor)
    {
        result.push(item);
    }
}
share|improve this answer

One solution to your problem is to search for the index that resides the admin username. In your case is at the 0 index of the json array provided. So you can get the entire object by the index, like this:

var i = 0;
for(; i< json.length; i++){
  if(json[i].user.username === "admin") break;
}

With that now you can get the object with the admin data. Like this:

json[i].user.firstName

Check this plunk here

EDIT If you want just to get that slice to a new array perhaps then you can just slice that piece of the json array, now that you have the index.

var newArray = json.slice(i, i+1);
share|improve this answer

You can use an open source project like jinqJs to perform SQL like queries on arrays.

var data = [
    {
        "_id": "5520f52e2c0a22541541bde1",
        "region": {
            "_id": "551e6779d8f1afa01bd86529",
            "name": "region_name"
        },
        "user": {
            "_id": "551a938af056a7fc099879c1",
            "firstName": "John",
            "lastName": "Boo",
            "username": "admin",
            "id": "551a938af056a7fc099879c1"
        },
        "__v": 0,
        "sport": [
            {
                "_id": "551e69c6d8f1afa01bd86533",
                "name": "Running"
            }
        ],
        "city": "some_city",
        "advert": "some_advert",
        "title": "I want to run!",
        "created": "2015-04-05T08:41:18.173Z"
    },
    {
        "_id": "552010740628cab002b3a700",
        "region": {
            "_id": "551e67b6d8f1afa01bd8652f",
            "name": "region_name"
        },
        "user": {
            "_id": "551a938af056a7fc099879c1",
            "firstName": "Bill",
            "lastName": "Foo",
            "username": "bill_foo",
            "id": "551a938af056a7fc099879c1"
        },
        "__v": 0,
        "sport": [
            {
                "_id": "551e5e01abb74a8423410b88",
                "nazev": "Hockey"
            }
        ],
        "city": "some_city",
        "advert": "some_advert",
        "title": "some_title",
        "created": "2015-04-04T16:25:24.733Z"
    }
];

var result = jinqJs()
                .from(data)
                .where(function(row){return row.user.username==='admin';})
                .select();

document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 4) + '</pre><br><br>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.