Stack Overflow Asked by Singh on October 3, 2020
const getDate = (id) =>{
const userId = info.find(user => user.id === id)
return userId.date;
}
const getValueAtOne = (id) => {
const userId = info.find(user => user.id === id)
if(userId?.value[0]){
return userId?.value[0]
}
}
const getValueAtTwo = (id) => {
const userId = info.find(user => user.id === id)
if(userId?.value[1]){
return userId?.value[1]
}
}
const getAllValues = (id) => {
const userId = info.find(user => user.id === id)
if(userId?.value) {
const arrValue = userId?.roles.map((validData) => {
return { value: validData, label:validData };
});
return arrValue;
}
}
I have these 4 methods, which I am calling from different places in my code.But i want to optimize my code and want all these methods in a single method, but I cant figure out the best way to do it. first method returns me the date, second one returns the value of array at position 1, third method returns value of array at position 2, and 4th method returns the all the value and convert it in object.
You can return a single object like so:
const getAll = (id) => {
const userId = info.find((user) => user.id === id);
const [valueAtOne, valueAtTwo] = userId?.value ?? [];
const allValues =
userId?.value.roles.map((validData) => ({
value: validData,
label: validData,
})) ?? [];
return {
allValues,
valueAtOne,
valueAtTwo,
};
};
Correct answer by Dennis Vash on October 3, 2020
0 Asked on January 5, 2021 by tu-ai-le
1 Asked on January 5, 2021 by clemson
1 Asked on January 5, 2021 by don-hatch
1 Asked on January 5, 2021 by benstackoverflow
2 Asked on January 5, 2021 by user3579222
4 Asked on January 4, 2021 by mrfacundo
2 Asked on January 4, 2021 by iliketocode123465
0 Asked on January 4, 2021 by adriano
0 Asked on January 4, 2021 by no-one
2 Asked on January 4, 2021 by ali-has
1 Asked on January 3, 2021 by 010110110101
0 Asked on January 3, 2021 by katie-schmidt
3 Asked on January 3, 2021 by snusifer
functional programming lisp list scheme tail call optimization
1 Asked on January 3, 2021 by ns68
0 Asked on January 3, 2021 by anna-bleha
Get help from others!
Recent Answers
Recent Questions
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP