Filter json data from a key

May 18, 2020 • edited May 19, 2020

You have a large flat JSON made up of many objects, made up of several key-values ​​(normal). You need to recompose that JSON so that the objects are filtered by one of its keys. It may sound extremely complex but trust me, with this feature you will reconsider.

Let’s start with this sample:

var miData = [
    {
        category: 'animales',
        name: 'Perro',
        pic: 'http://url.a/la/foto'
    },
    {
        category: 'cosas',
        name: 'Silla',
        pic: 'http://url.a/la/foto'
    },
    {
        category: 'cosas',
        name: 'Mesa',
        pic: 'http://url.a/la/foto'
    },
    {
        category: 'animales',
        name: 'Gato',
        pic: 'http://url.a/la/foto'
    },
    {
        category: 'cosas',
        name: 'Jarrón',
        pic: 'http://url.a/la/foto'
    }
];

Suppose we need to filter by category. At the end we will have the sample consisting of the keys animalesand cosaswhich in turn will contain the objects of each case.

NOTE: This feature is limited to just one filter because it is designed for one-off use. In our sample we filter by category.

For the effect, let’s extend Array.prototypewith the property keyFilterto be able to apply it more comfortably in our code:

Array.prototype.keyFilter = function (fil)
{
    var dat = this;
    var datFil = {};

    if ( fil )
    {
        for ( var i = 0, c = dat.length; i < c; i++ )
        {
            if ( !datFil[ dat[i][fil] ] ) datFil[ dat[i][fil] ] = [];
            datFil[ dat[i][fil] ].push( dat[i] );
        };
    };

    return datFil;
};

keyFilterIterate all the objects that make up the JSON to save the value of the key that we define filin the variable datFil. Those values ​​will then become the keys where the filtered objects will be saved.

Your JSON defines how many main keys the result makes up, just remember that you cannot define more than one filter in the arguments. Let’s see how to apply the function:

var miDataFiltrada = miData.keyFilter('category');
console.log( miDataFiltrada );
// RESPUESTA: Object {animales: Array[2], cosas: Array[3]}

This is our filtered JSON:

var miDataFiltrada = {
    animales: [
        {
            category: 'animales',
            name: 'Perro',
            pic: 'http://url.a/la/foto'
        },
        {
            category: 'animales',
            name: 'Gato',
            pic: 'http://url.a/la/foto'
        }
    ],
    cosas: [
        {
            category: 'cosas',
            name: 'Silla',
            pic: 'http://url.a/la/foto'
        },
        {
            category: 'cosas',
            name: 'Mesa',
            pic: 'http://url.a/la/foto'
        },
        {
            category: 'cosas',
            name: 'Jarrón',
            pic: 'http://url.a/la/foto'
        }
    ]
};

May the Force be with you. :-)

JavaScript

Junihh

Junihh is talk about web-dev and opinion.

Facebook makes me lose money

Forget about concat and start using concat