Grab some elements from a JSON variable

Asked

Viewed 488 times

2

I have a Javascript variable that stores a JSON, similar to this one:

{
    "estado":
    [
        {
             "nome":     "Foo LTDA",
             "endereco": "Endereço de SP",
             "cep":      "12345-000",
             "telefone": "(11) 1234-1234",
             "site":     "www.foo.com.br",
             "email":    "[email protected]"
        },
        {
             "nome":     "Foo LTDA",
             "endereco": "Endereço de SC",
             "cep":      "12345-000",
             "telefone": "(11) 1234-1234",
             "site":     "www.foo.com.br",
             "email":    "[email protected]"
        },
        {
             "nome":     "Foo LTDA",
             "endereco": "Endereço de RJ",
             "cep":      "12345-000",
             "telefone": "(11) 1234-1234",
             "site":     "www.foo.com.br",
             "email":    "[email protected]"
        }
    ]
}

Only with much more elements. How do I recover elements from 1 to 5, 6 to 10, 11 to 15 and so on?

  • You either get by interval the objects within state or will have multiple states?

1 answer

2


To search for range, you can use the .splice() to do this filtering in your array, example:

var yourJson = {
    "estado":
    [
        {
        ...
        }
    ]
}

result = yourJson.estado.splice(/*inicial*/ 1, /*final*/ 5);

console.log(result);

Remember to do the JSON.parse() from your Json to the variable :)

jsfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.