1

I set the array in onchange method and I see it in the console with console.log("array =",array) with all elemets but when I want to delete the duplicate element with Array.from(new Set(array)) method in the update method , I find error in the console ( cant access lexical declaration Array before initialization ) !

const [array, setArray] = useState([]); 

const update = (e) =>{ 

  e.preventDefault();  console.log("array =",array);  // = [ 1,  1, 2, 2, 51, 51 ] 
  
  if( array !==undefined){ 
  
    const Array = Array.from(new Set(array));    // error   } }
    
    setarticlesolde((pre) => {      
 
      setArray((pre) => [pre.articleId]);
      return  [... pre];
    })
3
  • Not much of your code makes sense. 1) array will always have a length of zero so: if (array.length) would be better. 2) Don't assign something to Array. Use another variable name. 3) What is artItem? 4) What is setarticlesolde, and why are you returning an array from it? Commented Apr 24, 2022 at 0:57
  • Change const Array = Array.from(new Set(array)); to const arr = Array.from(new Set(array)); and see if that fixes the problem Commented Apr 24, 2022 at 0:59
  • It workin when I add var arr =0; arr = Array.from(new Set(array)); Commented Apr 24, 2022 at 1:12

2 Answers 2

1

It workin when I change

const Array = Array.from(new Set(array));

to

var arr =0; arr = Array.from(new Set(array));

you cant use Array like variable because it already defined as existe function in javascript(React.js)

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

Comments

0

Simply change your code to const arr = Array.from(new Set(array)). Array is js inner variable.

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.