let List = [ { '4': 'dog' }, { '2': 'took' }, { '3': 'his' }, { '-2': 'Vatsan' }, { '5': 'for' }, { '6': 'a' }, { '12': 'spin' } ] function sentence(List) { let obj = {}; let str = ''; for (let i = 0; i < List.length; i++) { Object.assign(obj, List[i]) } let sortArr = Object.keys(obj).sort((a, b) => a - b); for (let i = 0; i < sortArr.length; i++) { str += obj[sortArr[i]] + ' ' } return str.trim() } console.log(sentence(List)) Another way const sentence = list => list .sort((a, b) => Object.keys(a)[0] - Object.keys(b)[0]) .map(item => Object.values(item)[0]) .join(' ') // Vatsan took his dog for a spin
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter