1

Hi all I am having a problem where I am getting this Json value from api

Json-

{
"level1":["a","b","c"],
"section":["a","b","c","d"],
"class":["a","b","c","d","e","f"],
"level2":["a","b"]
}

I need to re-arrage the level with value pair to a dropdown box in the format of

class
section
level1 
level2

this is the filter method to get all filters

filter = () => {
return Object.keys(this.props.Filters);
};

I am able to fetch the details from API but unable to re-arrange can anyone help me out on how to do this, since I am a very beginner to this it would be helpfull.

groot
  • 57
  • 3
  • 12

2 Answers2

1

One solution is to create a new object as below then pass it to your filter method.

const rearranged_object = {
    class: your_object.class,
    section: your_object.section,
    level1: your_object.level1,
    level2: your_object.level2
}

Check it out

Anh Nhat Tran
  • 561
  • 1
  • 6
  • 18
0

I've created a simple snippet for you -

var data = {
"level1":["a","b","c"],
"section":["a","b","c","d"],
"class":["a","b","c","d","e","f"],
"level2":["a","b"]
} 

var formattedData = Object.entries(data)

formattedData.sort(function (a, b) {
    return b[1].length - a[1].length ;
});

console.log(Object.fromEntries(formattedData))