Get some elements of a JSON variable

2

I have a JavaScript variable that holds a JSON, similar to this:

{
    "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 the one with many more elements. How do I retrieve elements from 1 to 5, 6 to 10, 11 to 15, and so on?

    
asked by anonymous 10.01.2017 / 18:27

1 answer

2

To search by range, you can use .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 JSON.parse() from your Json for the variable:)

jsfiddle

    
10.01.2017 / 18:40