具有项目类别的类别的项目数组

时间:2016-06-15 03:35:26

标签: javascript arrays object

如何将具有类别的项目数组转换为具有项目作为对象的类别?

输入项目数组

var items = [
    {
        itemId: 111111,
        title: 'Item-1',
        sellerName: 'seller-1',
        category: 'Electronics, Fitness'
    },
    {
        itemId: 222222,
        title: 'Item-2',
        sellerName: 'seller-2',
        category: 'Cars'
    },
    {
        itemId: 333333,
        title: 'Item-3',
        sellerName: 'seller-3',
        category: 'Electronics'
    },
    {
        itemId: 444444,
        title: 'Item-4',
        sellerName: 'seller-4',
        category: 'Cars, Bikes'
    }

]

//Result
    var result = {
        Electronics: [
            {
                itemId: 111111,
                title: 'Item-1',
                sellerName: 'seller-1' 
            },
            {
                itemId: 333333,
                title: 'Item-3',
                sellerName: 'seller-3'
            }
        ],
        Cars: [
            {
                itemId: 222222,
                title: 'Item-2',
                sellerName: 'seller-2'
            },
            {
                itemId: 444444,
                title: 'Item-4',
                sellerName: 'seller-4'
            }
        ],
        Bikes: [
            {
                itemId: 444444,
                title: 'Item-4',
                sellerName: 'seller-4'
            }
        ],

        Fitness: [
            {
                itemId: 111111,
                title: 'Item-1',
                sellerName: 'seller-1'
            }
        ]
    }

我尝试了这段代码,但并没有按照我想要的格式给出确切的结果:

function convertItemsToCategories(items){
var categories = {};
var itemarr = [];
for(var i =0; i < items.length; i++){
  //categories.push(items[i].category.split(','));
  var singleCategory = items[i].category.split(',');
  //categories[items[i].category] = items[i];
  singleCategory.forEach(function(catname){
    if(catname == items[i].category)
     itemarr.push(items[i]);
     categories[catname] = itemarr;
  });

  console.log(singleCategory);
}

console.log(categories);
}

convertItemsToCategories(items);

2 个答案:

答案 0 :(得分:1)

singleCategory.forEach(function(catname){
  if(catname == items[i].category)
   itemarr.push(items[i]);
   categories[catname] = itemarr;
});

此部件代码不正确。 catname是&#34;类别&#34;由&#34;,&#34;分隔category是原始类别。并且你继续推动itemarr中的项目而不清楚它,所以结果很奇怪。

请检查此jsfiddle

答案 1 :(得分:0)

if(catname == items[i].category)

不检查单个单词的单个单词。您可以在RegExp使用/\s|,/ .split()从数组,中移除空格和.filter()字符,并使用参数Boolean删除可能的空数组项目; Object.hasOwnProperty检查属性是否在对象上定义,如果不是在对象上创建属性,for..in循环设置属性和值,不包括category到新对象,.push()对象到数组

function convertItemsToCategories(items) {
  var categories = {};
  var itemarr = [];

  for(var i =0; i < items.length; i++){
    var currCat = items[i].category.split(/\s|,/).filter(Boolean);
    for (var j = 0; j < currCat.length; j++) {
      var obj = {};
      if (!categories.hasOwnProperty(currCat[j])) {
        categories[currCat[j]] = [];
      } 
      for (var prop in items[j]) {
        if (prop !== "category") {
          obj[prop] = items[j][prop]
        }
      }
      categories[currCat[j]].push(obj)       
    }
  }    
console.log(categories);
}