This might be stupid question however, I failed an interview because of this so asking. Does any one know how to clone an array of numbers into state. I have seen examples of objects. I have tried spread operator no luck, tried JSON.parse no luck.
const [Sorted,setSorted]=useState([])
let a=[1,3,5]
let b=[2,4,6]
function sorts(){
let c=[...a,...b];
return(c.sort());//it will return[1,2,3,4,5,6]
}
useEffect(()=>{
let sortedArr=sorts();
setSorted([...sortedArr]);//No use
setSorted(JSON.parse(JSON.stringify(sortedArr));//No use
},[])
//Printing using Map
Please ignore any typing mistakes. Thank you