对象数组将相同的值分隔为另一个数组的键

时间:2018-06-28 09:09:21

标签: javascript arrays json

我正在尝试使用javascript处理下面给出的JSON,该javascript具有产品列表和每种产品的类别。

data = [
  {
    "item_id": "1233131",
    "item_name": "cabbage",
    "item_price": "20$",
    "item_category": "vegetables"
  },
  {
    "item_id": "1233232",
    "item_name": "Tomato",
    "item_price": "15$",
    "item_category": "fruits"
  },
  {
    "item_id": "34323411",
    "item_name": "carrot",
    "item_price": "10$",
    "item_category": "vegetables"
  }
];

我要在其中划分类别并附加所有属于该类别的产品

我想要实现的期望结果(如JSON格式):

[
  {
    "title": "vegetables",
    "content": [
      {
        "item_id": "34323411",
        "item_name": "carrot",
        "item_price": "10$"
      },
      {
        "item_id": "1233131",
        "item_name": "cabbage",
        "item_price": "20$"
      }
    ]
  },
  {
    "title": "fruits",
    "content": [
      {
        "item_id": "1233232",
        "item_name": "Tomato",
        "item_price": "15$"
      }
    ]
  }
]

已编辑:到目前为止,我已经尝试过。

var newArray = [];
var result = [];
data.forEach(item => {
  newArray.push({
    title: item.item_category,
  });
});

newArray.filter(arrdat => {
  console.log(arrdat.title);
  data.forEach(tempDat => {
    if (arrdat.title === tempDat.item_category) {
      result.push({
        title: arrdat.title,
        category: tempDat
      });
      console.log(result);
    }
  });
});

2 个答案:

答案 0 :(得分:1)

首先将其分组,然后映射以获得所需的结构

const res = [...data.reduce((a, {item_category, ...b}) =>
    a.set(item_category, (a.get(item_category) || []).concat(b)), new Map)]
  .map(([title, content]) => ({
    title,
    content
  }));

console.log(res);
<script>
  const data = [{
      "item_id": "1233131",
      "item_name": "cabbage",
      "item_price": "20$",
      "item_category": "vegetables"
    },
    {
      "item_id": "1233232",
      "item_name": "Tomato",
      "item_price": "15$",
      "item_category": "fruits"
    },
    {
      "item_id": "34323411",
      "item_name": "carrot",
      "item_price": "10$",
      "item_category": "vegetables"
    }
  ];
</script>

答案 1 :(得分:1)

let s = {};
let structuredData = [];
let arr = [

{
    "item_id": "1233131",
    "item_name": "cabbage",
    "item_price": "20$",
    "item_category": "vegetables"
  },
  {
    "item_id": "1233232",
    "item_name": "Tomato",
    "item_price": "15$",
    "item_category": "fruits"
  },
  {
    "item_id": "34323411",
    "item_name": "carrot",
    "item_price": "10$",
    "item_category": "vegetables"
  }
];

arr.forEach(a => {
	if (s[a.item_category]){
    	s[a.item_category].push(a);
    } else {
    	s[a.item_category] = [a];
    }
    delete a.item_category;
});
for (let key in s) {
	structuredData.push({title: key, content: s[key]})
}
console.log(structuredData);

let s = {};
let structuredData = [];
let arr = [

{
    "item_id": "1233131",
    "item_name": "cabbage",
    "item_price": "20$",
    "item_category": "vegetables"
},
{
    "item_id": "1233232",
    "item_name": "Tomato",
    "item_price": "15$",
    "item_category": "fruits"
},
{
    "item_id": "34323411",
    "item_name": "carrot",
    "item_price": "10$",
    "item_category": "vegetables"
}];

arr.forEach(a => {
    if (s[a.item_category]){
        s[a.item_category].push(a);
    } else {
        s[a.item_category] = [a];
    }
});
for (let key in s) {
    structuredData.push({title: key, content: s[key]})
}