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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.