简体   繁体   English

从JSON创建对象数组

[英]Create an array of Objects from JSON

I have a JSON file like below: 我有一个如下的JSON文件:

[

{"fields":{category_class":"CAT2",category_name":"A"},"pk":1 },

{"fields":{category_class":"CAT1",category_name":"B"},"pk":2 },

{"fields":{category_class":"CAT1",category_name":"C"},"pk":3 },

{"fields":{category_class":"CAT2",category_name":"D"},"pk":4 },

{"fields":{category_class":"CAT3",category_name":"E"},"pk":5 },

{"fields":{category_class":"CAT1",category_name":"E"},"pk":6 },

]

I want to create an array of objects from the above JSON which will have two properties. 我想从上面的JSON创建一个对象数组,它将具有两个属性。 i) CategoryClass ii) CategoryNameList. i)CategoryClass ii)CategoryNameList。 For example: 例如:

this.CategoryClass = "CAT1"
this.CategoryNameList = ['B','C','E']

Basically i want to select all categories name whose category class is CAT1 and so forth for other categories class. 基本上,我想选择类别类别为CAT1的所有类别名称,以此类推。 I tried this: 我尝试了这个:

var category = function(categoryClass, categoryNameList){

this.categoryClass = categoryClass;
this.categoryList = categoryNameList;

}

var categories = [];

categories.push(new category('CAT1',['B','C','E'])

Need help. 需要帮忙。

You can use a simple filter on the array. 您可以在阵列上使用简单的过滤器。 You have a few double quotes that will cause an error in you code. 您有一些双引号,这会在您的代码中导致错误。 But to filter only with CAT1 you can use the filter method 但是要仅使用CAT1进行过滤,可以使用过滤方法

var cat1 = arr.filter( value => value.fields.category_class === "CAT1");

Question : Basically i want to select all categories name whose category class is CAT1 and so forth for other categories class 问题 :基本上,我想选择类别类别为CAT1的所有类别名称,以此类推。

Solution : 解决方案

function Select_CatName(catclass,array){
  var CatNameList=[]
  $(array).each(function(){
  if(this.fields.category_class==catclass)
    CatNameList.push(this.fields.category_name)
 })
return CatNameList;
}

This function return the Desired Category Name List, you need to pass desired catclass and array of the data , as in this case it's your JSON. 此函数返回所需的类别名称列表,您需要传递所需的catclass和数据数组,因为在这种情况下,这是您的JSON。

Input : 输入

在此处输入图片说明

Above function calling : 上面的函数调用

在此处输入图片说明

Output : 输出

在此处输入图片说明

Hope It helps. 希望能帮助到你。

I would suggest this ES6 function, which creates an object keyed by category classes, providing the object with category names for each: 我建议使用此ES6函数,该函数创建一个以类别类为键的对象,并为每个对象提供类别名称:

 function groupByClass(data) { return data.reduce( (acc, { fields } ) => { (acc[fields.category_class] = acc[fields.category_class] || { categoryClass: fields.category_class, categoryNameList: [] }).categoryNameList.push(fields.category_name); return acc; }, {} ); } // Sample data var data = [ {"fields":{"category_class":"CAT2","category_name":"A"},"pk":1 }, {"fields":{"category_class":"CAT1","category_name":"B"},"pk":2 }, {"fields":{"category_class":"CAT1","category_name":"C"},"pk":3 }, {"fields":{"category_class":"CAT2","category_name":"D"},"pk":4 }, {"fields":{"category_class":"CAT3","category_name":"E"},"pk":5 }, {"fields":{"category_class":"CAT1","category_name":"E"},"pk":6 }, ]; // Convert var result = groupByClass(data); // Outut console.log(result); // Example look-up: console.log(result['CAT1']); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM