Create a select option in the react js from an object










0















enter image description hereenter image description hereI am new to the react-redux. I actually know this question is a very basic question,



Here, I have an object which is like,



questionsCount : [ 
Id : "CSS"
count: "1",
level:"Easy",
type: Non_code,
Id : "CSS"
count: "2",
level:"Medium",
type: code,

Id : "CSS"
count: "5",
level:"Tough",
type: code,

Id : "java"
count: "2",
level:"Tough",
type: Non-code,

Id : "Html"
count: "4",
level:"Easy",
type: code,]


Now,I have one select drop-down where I want to create an options , So I tried



return this.state.questionsCount.map((item) =>
<option key=item.Id>item.Id</option>
);

In the props I get the level type.


Now, Here what its creating all the option elements with repetative Id's.



Now, What I want to do is that,



the option element will be like, first it should have only provided level elements meanes,



css code:0,Non_code:1


So,Currently Its getting duplicated .



Its getting duplicated because Id is getting repeated. Can any one help me with this ? Thanks



Here,I have that object. Now In that object there are multiple objects which has Id and count and level and type .



Now, from this objects I want to create the select options from this.



It has three diff levels.



expected Output would be like ,
It will be having only easy level



<option>Css code:0,non_code-1></option>
<option>Html code:4,non_code-0></option>


for tough options,



<option>Css code:5,non_code-1></option>
<option>java code:0,non_code-2></option>


same for medium.and tough.



One technology can have code type or non code type questions of type easy or medium or tough. So, I want to create the drop-down of that technolgy depends upon the levels.



If code or non-code not present then it will have 0 for that.










share|improve this question



















  • 1





    Not able to understand what exactly you want to achieve. Could you show your expected result

    – Shubham Khatri
    Nov 13 '18 at 6:18











  • okay will add that

    – ganesh
    Nov 13 '18 at 6:21











  • @ShubhamKhatri I have just updated my code

    – ganesh
    Nov 13 '18 at 6:31











  • One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

    – ganesh
    Nov 13 '18 at 6:34











  • @ShubhamKhatri did you get what I want to ask ?

    – ganesh
    Nov 13 '18 at 7:13















0















enter image description hereenter image description hereI am new to the react-redux. I actually know this question is a very basic question,



Here, I have an object which is like,



questionsCount : [ 
Id : "CSS"
count: "1",
level:"Easy",
type: Non_code,
Id : "CSS"
count: "2",
level:"Medium",
type: code,

Id : "CSS"
count: "5",
level:"Tough",
type: code,

Id : "java"
count: "2",
level:"Tough",
type: Non-code,

Id : "Html"
count: "4",
level:"Easy",
type: code,]


Now,I have one select drop-down where I want to create an options , So I tried



return this.state.questionsCount.map((item) =>
<option key=item.Id>item.Id</option>
);

In the props I get the level type.


Now, Here what its creating all the option elements with repetative Id's.



Now, What I want to do is that,



the option element will be like, first it should have only provided level elements meanes,



css code:0,Non_code:1


So,Currently Its getting duplicated .



Its getting duplicated because Id is getting repeated. Can any one help me with this ? Thanks



Here,I have that object. Now In that object there are multiple objects which has Id and count and level and type .



Now, from this objects I want to create the select options from this.



It has three diff levels.



expected Output would be like ,
It will be having only easy level



<option>Css code:0,non_code-1></option>
<option>Html code:4,non_code-0></option>


for tough options,



<option>Css code:5,non_code-1></option>
<option>java code:0,non_code-2></option>


same for medium.and tough.



One technology can have code type or non code type questions of type easy or medium or tough. So, I want to create the drop-down of that technolgy depends upon the levels.



If code or non-code not present then it will have 0 for that.










share|improve this question



















  • 1





    Not able to understand what exactly you want to achieve. Could you show your expected result

    – Shubham Khatri
    Nov 13 '18 at 6:18











  • okay will add that

    – ganesh
    Nov 13 '18 at 6:21











  • @ShubhamKhatri I have just updated my code

    – ganesh
    Nov 13 '18 at 6:31











  • One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

    – ganesh
    Nov 13 '18 at 6:34











  • @ShubhamKhatri did you get what I want to ask ?

    – ganesh
    Nov 13 '18 at 7:13













0












0








0








enter image description hereenter image description hereI am new to the react-redux. I actually know this question is a very basic question,



Here, I have an object which is like,



questionsCount : [ 
Id : "CSS"
count: "1",
level:"Easy",
type: Non_code,
Id : "CSS"
count: "2",
level:"Medium",
type: code,

Id : "CSS"
count: "5",
level:"Tough",
type: code,

Id : "java"
count: "2",
level:"Tough",
type: Non-code,

Id : "Html"
count: "4",
level:"Easy",
type: code,]


Now,I have one select drop-down where I want to create an options , So I tried



return this.state.questionsCount.map((item) =>
<option key=item.Id>item.Id</option>
);

In the props I get the level type.


Now, Here what its creating all the option elements with repetative Id's.



Now, What I want to do is that,



the option element will be like, first it should have only provided level elements meanes,



css code:0,Non_code:1


So,Currently Its getting duplicated .



Its getting duplicated because Id is getting repeated. Can any one help me with this ? Thanks



Here,I have that object. Now In that object there are multiple objects which has Id and count and level and type .



Now, from this objects I want to create the select options from this.



It has three diff levels.



expected Output would be like ,
It will be having only easy level



<option>Css code:0,non_code-1></option>
<option>Html code:4,non_code-0></option>


for tough options,



<option>Css code:5,non_code-1></option>
<option>java code:0,non_code-2></option>


same for medium.and tough.



One technology can have code type or non code type questions of type easy or medium or tough. So, I want to create the drop-down of that technolgy depends upon the levels.



If code or non-code not present then it will have 0 for that.










share|improve this question
















enter image description hereenter image description hereI am new to the react-redux. I actually know this question is a very basic question,



Here, I have an object which is like,



questionsCount : [ 
Id : "CSS"
count: "1",
level:"Easy",
type: Non_code,
Id : "CSS"
count: "2",
level:"Medium",
type: code,

Id : "CSS"
count: "5",
level:"Tough",
type: code,

Id : "java"
count: "2",
level:"Tough",
type: Non-code,

Id : "Html"
count: "4",
level:"Easy",
type: code,]


Now,I have one select drop-down where I want to create an options , So I tried



return this.state.questionsCount.map((item) =>
<option key=item.Id>item.Id</option>
);

In the props I get the level type.


Now, Here what its creating all the option elements with repetative Id's.



Now, What I want to do is that,



the option element will be like, first it should have only provided level elements meanes,



css code:0,Non_code:1


So,Currently Its getting duplicated .



Its getting duplicated because Id is getting repeated. Can any one help me with this ? Thanks



Here,I have that object. Now In that object there are multiple objects which has Id and count and level and type .



Now, from this objects I want to create the select options from this.



It has three diff levels.



expected Output would be like ,
It will be having only easy level



<option>Css code:0,non_code-1></option>
<option>Html code:4,non_code-0></option>


for tough options,



<option>Css code:5,non_code-1></option>
<option>java code:0,non_code-2></option>


same for medium.and tough.



One technology can have code type or non code type questions of type easy or medium or tough. So, I want to create the drop-down of that technolgy depends upon the levels.



If code or non-code not present then it will have 0 for that.







javascript reactjs redux react-redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:44







ganesh

















asked Nov 13 '18 at 6:13









ganeshganesh

1658




1658







  • 1





    Not able to understand what exactly you want to achieve. Could you show your expected result

    – Shubham Khatri
    Nov 13 '18 at 6:18











  • okay will add that

    – ganesh
    Nov 13 '18 at 6:21











  • @ShubhamKhatri I have just updated my code

    – ganesh
    Nov 13 '18 at 6:31











  • One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

    – ganesh
    Nov 13 '18 at 6:34











  • @ShubhamKhatri did you get what I want to ask ?

    – ganesh
    Nov 13 '18 at 7:13












  • 1





    Not able to understand what exactly you want to achieve. Could you show your expected result

    – Shubham Khatri
    Nov 13 '18 at 6:18











  • okay will add that

    – ganesh
    Nov 13 '18 at 6:21











  • @ShubhamKhatri I have just updated my code

    – ganesh
    Nov 13 '18 at 6:31











  • One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

    – ganesh
    Nov 13 '18 at 6:34











  • @ShubhamKhatri did you get what I want to ask ?

    – ganesh
    Nov 13 '18 at 7:13







1




1





Not able to understand what exactly you want to achieve. Could you show your expected result

– Shubham Khatri
Nov 13 '18 at 6:18





Not able to understand what exactly you want to achieve. Could you show your expected result

– Shubham Khatri
Nov 13 '18 at 6:18













okay will add that

– ganesh
Nov 13 '18 at 6:21





okay will add that

– ganesh
Nov 13 '18 at 6:21













@ShubhamKhatri I have just updated my code

– ganesh
Nov 13 '18 at 6:31





@ShubhamKhatri I have just updated my code

– ganesh
Nov 13 '18 at 6:31













One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

– ganesh
Nov 13 '18 at 6:34





One thing is that, In the props I get the props.type which will tell that currently what type we need to iterate. I mean Easy or tough or medium

– ganesh
Nov 13 '18 at 6:34













@ShubhamKhatri did you get what I want to ask ?

– ganesh
Nov 13 '18 at 7:13





@ShubhamKhatri did you get what I want to ask ?

– ganesh
Nov 13 '18 at 7:13












1 Answer
1






active

oldest

votes


















2














From what I understand in your case, you wish to create a select dropdown based on the difficulty level. To achieve that, you would need to filter our the question based on difficult first before rendering like



processQuestions(questionsCount, difficulty) 
const res = questionsCount.filter(item => item.level === difficulty);
return res.reduce((acc, item) =>
if (acc[item.Id])
acc[item.Id] = ...acc[item.Id], [item.type]: acc[item.Id][item.type]? acc[item.Id][item.type] + 1 : 1;
else
acc[item.Id] = [item.type]: 1;

return acc;
, )

render()
const difficulty = this.props;
const questionsData = this.processQuestions(this.state.questionCount, difficulty);
return Object.entries(questionsData).map(([questionId, data]) =>
return (
<option key=questionId>questionId code: data.code non_code: data['Non-code']</option>
)
)






share|improve this answer

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 13 '18 at 22:05











  • @shubhamKhatri thanks for your solution. Worked perfectly

    – ganesh
    Nov 14 '18 at 10:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274865%2fcreate-a-select-option-in-the-react-js-from-an-object%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














From what I understand in your case, you wish to create a select dropdown based on the difficulty level. To achieve that, you would need to filter our the question based on difficult first before rendering like



processQuestions(questionsCount, difficulty) 
const res = questionsCount.filter(item => item.level === difficulty);
return res.reduce((acc, item) =>
if (acc[item.Id])
acc[item.Id] = ...acc[item.Id], [item.type]: acc[item.Id][item.type]? acc[item.Id][item.type] + 1 : 1;
else
acc[item.Id] = [item.type]: 1;

return acc;
, )

render()
const difficulty = this.props;
const questionsData = this.processQuestions(this.state.questionCount, difficulty);
return Object.entries(questionsData).map(([questionId, data]) =>
return (
<option key=questionId>questionId code: data.code non_code: data['Non-code']</option>
)
)






share|improve this answer

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 13 '18 at 22:05











  • @shubhamKhatri thanks for your solution. Worked perfectly

    – ganesh
    Nov 14 '18 at 10:07















2














From what I understand in your case, you wish to create a select dropdown based on the difficulty level. To achieve that, you would need to filter our the question based on difficult first before rendering like



processQuestions(questionsCount, difficulty) 
const res = questionsCount.filter(item => item.level === difficulty);
return res.reduce((acc, item) =>
if (acc[item.Id])
acc[item.Id] = ...acc[item.Id], [item.type]: acc[item.Id][item.type]? acc[item.Id][item.type] + 1 : 1;
else
acc[item.Id] = [item.type]: 1;

return acc;
, )

render()
const difficulty = this.props;
const questionsData = this.processQuestions(this.state.questionCount, difficulty);
return Object.entries(questionsData).map(([questionId, data]) =>
return (
<option key=questionId>questionId code: data.code non_code: data['Non-code']</option>
)
)






share|improve this answer

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 13 '18 at 22:05











  • @shubhamKhatri thanks for your solution. Worked perfectly

    – ganesh
    Nov 14 '18 at 10:07













2












2








2







From what I understand in your case, you wish to create a select dropdown based on the difficulty level. To achieve that, you would need to filter our the question based on difficult first before rendering like



processQuestions(questionsCount, difficulty) 
const res = questionsCount.filter(item => item.level === difficulty);
return res.reduce((acc, item) =>
if (acc[item.Id])
acc[item.Id] = ...acc[item.Id], [item.type]: acc[item.Id][item.type]? acc[item.Id][item.type] + 1 : 1;
else
acc[item.Id] = [item.type]: 1;

return acc;
, )

render()
const difficulty = this.props;
const questionsData = this.processQuestions(this.state.questionCount, difficulty);
return Object.entries(questionsData).map(([questionId, data]) =>
return (
<option key=questionId>questionId code: data.code non_code: data['Non-code']</option>
)
)






share|improve this answer















From what I understand in your case, you wish to create a select dropdown based on the difficulty level. To achieve that, you would need to filter our the question based on difficult first before rendering like



processQuestions(questionsCount, difficulty) 
const res = questionsCount.filter(item => item.level === difficulty);
return res.reduce((acc, item) =>
if (acc[item.Id])
acc[item.Id] = ...acc[item.Id], [item.type]: acc[item.Id][item.type]? acc[item.Id][item.type] + 1 : 1;
else
acc[item.Id] = [item.type]: 1;

return acc;
, )

render()
const difficulty = this.props;
const questionsData = this.processQuestions(this.state.questionCount, difficulty);
return Object.entries(questionsData).map(([questionId, data]) =>
return (
<option key=questionId>questionId code: data.code non_code: data['Non-code']</option>
)
)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 11:38

























answered Nov 13 '18 at 7:37









Shubham KhatriShubham Khatri

79.6k1496133




79.6k1496133












  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 13 '18 at 22:05











  • @shubhamKhatri thanks for your solution. Worked perfectly

    – ganesh
    Nov 14 '18 at 10:07

















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 13 '18 at 22:05











  • @shubhamKhatri thanks for your solution. Worked perfectly

    – ganesh
    Nov 14 '18 at 10:07
















Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 13 '18 at 22:05





Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 13 '18 at 22:05













@shubhamKhatri thanks for your solution. Worked perfectly

– ganesh
Nov 14 '18 at 10:07





@shubhamKhatri thanks for your solution. Worked perfectly

– ganesh
Nov 14 '18 at 10:07

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274865%2fcreate-a-select-option-in-the-react-js-from-an-object%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3