Created
February 1, 2017 07:03
-
-
Save AshikNesin/5a19248e634d38e6d17ab40743c69f3b to your computer and use it in GitHub Desktop.
Insert/Update Items - Filter & Map Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let jobs = [{id:44,selected:false},{id:12,selected:true}] | |
const handleChange = (item) =>{ | |
let existItemIndex; | |
jobs.map((job,index)=>{ | |
(job.id === item.id) && (existItemIndex = index) | |
}) | |
if(!existItemIndex){ | |
jobs = jobs.concat(item) | |
} | |
else{ | |
jobs[existItemIndex].selected = item.selected | |
} | |
const filteredJobs = jobs.filter((item)=>{ | |
return item.selected | |
}) | |
const selectedJobs = []; | |
filteredJobs.map((job)=>selectedJobs.push(job.id)) | |
console.log(selectedJobs) | |
} | |
handleChange({id:12,selected:true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment