Michel Conrado Diz
Michel Conrado Diz

Reputation: 321

Select a prop of an object with arrays

So, I have this data coming from a REST API in JSON. I'll put here as an example.

var JsonCategoriaHot = {
    "products": [
        {"id": 8,
         "id_manufacturer": "0",
         "manufacturer_name": "Fashion Manufacturer",
         "id_supplier": "0",
         "id_category_default": "Isso terá de ser uma URL ou um nome",
         "new": "null",
         "images": "ur.presta.com",
         "name": [{"id": "1", "value": "Printed Summer Dress"},
                  {"id": "2", "value": "Vestido estampado de verão"}],
         "description": [{"id":"1","value":"Ensaio do Muzenza"}],},
        {"id":1}, // just empty data to represent "something"
        {"id":2},
        {"id":3},
        {"id":4},
        {"id":5},
        {"id":9,
         "name": "Michel Conrado",
         "images": "ur.presta.com/michelconrado",
         "manufacturer_name": "Muzenza",
         "category_default": "mudar de cat_default para url",
         "description": "Essa é uma descrição"
        },
        {"id":12,
         "name": "12 Michel Conrado",
         "images": "ur.presta.com/michelconrado",
         "manufacturer_name": "Muzenza",
         "category_default": "mudar de cat_default para url",
         "description": "Essa é uma descrição"
        }
    ]
};

So from this data I want to map some objects. And change some strings name. With this code below:

var idparaurl = JsonCategoriaHot.products.map(function(products,i) {
    return {
        index: i + 1,
        name: products.name,
        image:  products.images,
        fabricante: products.manufacturer_name,
        catgdf: products.id_category_default,
        desc: products.description,
    };
});

Now I need to "select" the property "name" choosing a default id for this case will be "Products" > "Name" > ID > 2 > value > String.

It's like a "reduce" and choose a value from the "name" array.

Maybe in the future will be more languages to support. So Each ID represents an translation from my CMS for each correspond languages.

It should return (for each object - all mapped data will come like this):

[
    {
        "catgdf": "Isso terá de ser uma URL ou um nome tidpo Muzenza",
        "image": "ur.presta.com",
        "index": 1,
        "name":  "Vestido estampado de verão",
        "desc": [
            {
                "id": "1",
                "value": "Ensaio do Muzenza"
            }
        ],
        "fabricante": "Fashion Manufacturer"
    }

    //...
]

The "description" array I will apply this solution also so I should be like this inside the obj:

    //...
    "desc": "Ensaio do Muzenza"
}

Upvotes: 1

Views: 87

Answers (1)

Raj Nathani
Raj Nathani

Reputation: 2845

I would suggest using a filter operation as opposed to using a reduce.

Here is a function which would work:

function parseArrayKeyIndexValue(valArray, id) {
  if (!(valArray instanceof Array)) {
    return valArray;
  }
  return valArray.filter(function(valArrayItem){
    if (valArrayItem.id === id) {
      return true;
    }
  })[0].value;
}

The map in your example would look like this:

var idparaurl = JsonCategoriaHot.products.map(function(products,i) {
    return {
        index: i + 1,
        name: parseArrayKeyIndexValue(products.name, "2"),
        image:  products.images,
        fabricante: products.manufacturer_name,
        catgdf: products.id_category_default,
        desc: parseArrayKeyIndexValue(products.description, "1"),
    };
});

The code can be run with your provided 'JsonCategoriaHot' example data at this jsbin: http://jsbin.com/zapimineje/edit?js,console,output

Upvotes: 1

Related Questions