How to take objects from an array, with filter passing as parameter an object with multiple ID’s

Asked

Viewed 1,249 times

3

Associating the object "categories" in "products"; With the ID I need to associate to such product, so for example "products 1" should receive the "categories" with the ID [1, 3, 4]. creating another array combining the data between these objects.

{
    "produtos": [{
        "id": 1,
            "nome": "Produto 1",
            "categorias": [1, 3, 4]
    }, {
        "id": 2,
            "nome": "Produto 2",
            "categorias": [1, 2, 5]
    }, {
        "id": 3,
            "nome": "Produto 3",
            "categorias": [3, 1, 4]
    }],
        "categorias": [{
        "id": 1,
            "nome": "Categoria 1"
    }, {
        "id": 2,
            "nome": "Categoria 2"
    }, {
        "id": 3,
            "nome": "Categoria 3"
    }, {
        "id": 4,
            "nome": "Categoria 4"
    }, {
        "id": 5,
            "nome": "Categoria 5"
    }]
}

Result array: { "products": [ { "id": 1, "name": "Product 1", "categories": [ {"id" : 1, "name" : "Category 1"}, {"id" : 3, "name" : "Category 3"}, {"id" : 4, "name" : "Category 4"} ] } ]

  • 1

    Can you explain better what’s in the title? It’s unclear what you want to do...

  • With object "categories" in "products" with the ID I need to associate to such product, so for example "products 1" should display the "categories" with the ID [1, 3, 4].

  • It’s still unclear... Can you make [Edit]ar on the question and add more info? I don’t understand what you mean by "should show" and "with filter". Want to generate HTML or create another array combining the data this object?

  • You can add an example of what the final result should look like?

  • 1

    @Sergio, I made the change, it’s something like associating the object categories to the object products, where I have the ID of the categories of which will be combined

1 answer

4


If I understand what you want to test like this:

var json = { "produtos": [{ "id": 1, "nome": "Produto 1", "categorias": [1, 3, 4] }, { "id": 2, "nome": "Produto 2", "categorias": [1, 2, 5] }, { "id": 3, "nome": "Produto 3", "categorias": [3, 1, 4] }], "categorias": [{ "id": 1, "nome": "Categoria 1" }, { "id": 2, "nome": "Categoria 2" }, { "id": 3, "nome": "Categoria 3" }, { "id": 4, "nome": "Categoria 4" }, { "id": 5, "nome": "Categoria 5" }] };

json = (function () {
    json.produtos.forEach(function (produtoObjeto) {
        produtoObjeto.categorias = produtoObjeto.categorias.map(function(id) {
            var categoria = json.categorias.filter(function(objeto) {
                return objeto.id == id;
            })[0];
            return categoria;
        });
    })
    return json;
})();
alert(JSON.stringify(json, null, 4));

Dividing by parts what the code does:

  • json.produtos.forEach(function (produtoObjeto) {

Iterate over the product array

  • produtoObjeto.categorias = produtoObjeto.categorias.map(function(id) {

will map the array of categories within each product. It will swap each number within that array with the corresponding object in the array of categories.

  • var categoria = json.categorias.filter(function(objeto) {

within the array of categories the .filter() will only return the one who has the right ID

  • 1

    Thanks @Sergio, that was exactly it, I spent days, looking for solutions, thanks so much.

Browser other questions tagged

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