Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Automatically select next item in array

I have a button that deletes a note, and it is supposed to automatically select the next note after the first note is deleted. However, it is not working, and i have this error Uncaught TypeError: Cannot set properties of undefined (setting 'selected')

import React, { useContext } from 'react';
import trashicon from '../../trashicon.png'
import {NoteContext} from '../../noteContext';

const DeleteNote = () => {
    const [notes, setNotes] = useContext(NoteContext);

    const handleDeleteNote =()=> {
        const newNotesList = notes.filter(itemChecked=>itemChecked.selected===false)
        console.log(newNotesList[0])
        for (let i = 0; i < notes.length; i++) {
            if(notes[i].selected === true) {
                newNotesList[i].selected = true;
            }
        
        }
        console.log(newNotesList);
        setNotes(newNotesList);

    }
    return (

            <img src={trashicon} className="delete-icon" onClick={handleDeleteNote}/>

    )
}
export default DeleteNote
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This logic is quite incorrect, what if notes arr has 5 objects, and newNotesList returns 3 different objects, then when you loop it will update first 3 incorrect objects in the newNotesList array.

IMO, this should be the correct logic.

if (!newNotesList.length) return;
for (let i = 0; i < notes.length; i++) {
    //
}

if(notes[i].selected === true) {
    const findElementInNewNotesList = newNotesList.find(el => el.id === notes[i].id);
    if(findElementInNewNotesList.id) newNotesList[i].selected = true;
}

This way you're finding the matching object ONLY from newNotesList.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!