let arr = [3,1,3,2,5,4,5] let mapA = [] function getDD (arr,k){ arr.map((item,i)=>{ console.log(item) mapA.map((jj,j)=>{ if(item == jj.id){ mapA[j].value+=(","+jj.value) // return false } }) mapA.push({"id":item,"value":item}) }) return mapA[k].value } console.log(getDD(arr,4))