选择具有数组的对象的prop

时间:2017-02-03 17:58:09

标签: javascript arrays

因此,我将此数据来自JSON中的REST API。我将把这里作为一个例子。

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"
        }
    ]
};

所以从这些数据中我想映射一些对象。并更改一些字符串名称。使用以下代码:

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,
    };
});

现在我需要"选择"财产"名称"选择此案例的默认ID将是"产品" > "名称" > ID> 2>值>字符串。

它就像是"减少"并从" name"中选择一个值。阵列。

将来可能会支持更多语言。因此,每个ID代表我的CMS对每种对应语言的翻译。

它应该返回(对于每个对象 - 所有映射的数据都将如下所示):

[
    {
        "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"
    }

    //...
]

"描述"数组我也将应用这个解决方案所以我应该在obj中这样:

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

1 个答案:

答案 0 :(得分:1)

我建议使用filter操作,而不是使用reduce

这是一个可行的功能:

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

示例中的地图如下所示:

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"),
    };
});

代码可以使用您提供的' JsonCategoriaHot'此jsbin的示例数据:http://jsbin.com/zapimineje/edit?js,console,output