1

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"
}]
Jaroslav Klimčík
  • 4,548
  • 12
  • 39
  • 58
  • Please include the expected output. – thefourtheye Apr 05 '15 at 09:50
  • show some code you've tried – Amit Joki Apr 05 '15 at 09:51
  • use jquery .each etc : [jquery loop on Json data using $.each][1] [1]: http://stackoverflow.com/questions/2342371/jquery-loop-on-json-data-using-each – Javad Khodadadi Apr 05 '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](http://stackoverflow.com/a/29456146/1960455). – t.niese Apr 05 '15 at 10:10

3 Answers3

4

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);
    }
}
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
0

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);
gdyrrahitis
  • 5,598
  • 3
  • 23
  • 37
0

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>
NYTom
  • 524
  • 2
  • 14