0

I have a JSON file like below:

[

{"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. 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. 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.

3 Answers 3

1

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

var cat1 = arr.filter( value => value.fields.category_class === "CAT1");
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest this ES6 function, which creates an object keyed by category classes, providing the object with category names for each:

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']);

Comments

0

Question : Basically i want to select all categories name whose category class is CAT1 and so forth for other categories class

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.

Input :

enter image description here

Above function calling :

enter image description here

Output :

enter image description here

Hope It helps.

2 Comments

It would be better not to include images of content that really is text.
@trincot ohkay !! i will remember next time .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.