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"
}]
4
  • Please include the expected output. Commented Apr 5, 2015 at 9:50
  • show some code you've tried Commented Apr 5, 2015 at 9:51
  • use jquery .each etc : [jquery loop on Json data using $.each][1] [1]: stackoverflow.com/questions/2342371/… Commented Apr 5, 2015 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. Commented Apr 5, 2015 at 10:10

3 Answers 3

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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>

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.