Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

returns string of names, seperated by commas and an ampersand namesObj in javascript

function list(names){
  let answer = [];
  let result;
  
  if (names.length === 0) return '';

  for (let n in names) {
    const values = Object.values(names[n]).join('')

    if (names.length === 1) {
      return values
    } else if (names.length === 2 ) {
      answer.push(values)
      result = answer.join(' & ')
    } else {
      answer.push(values);
      result = answer.join(', ')
      let n = result.lastIndexOf(', ')
      result = result.slice(0, n) + result.slice(n).replace(', ', ' & ');
    }
  }
 
  return result
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: parse youtu.be url and get time 
Javascript :: mongo look for substring of field 
Javascript :: javascript pure ajax promise 
Javascript :: dynamic copyright year JavaScript centre aligned 
Javascript :: reference to javascript array 
Javascript :: how to manually sort array javascript 
Javascript :: limit ajax request 
Javascript :: .catch() in promise will aslo return a promise 
Javascript :: nuxt auth no provider 
Javascript :: javascript change color of button onclick 
Javascript :: what is fn extend 
Javascript :: prepare webpack-ready 
Javascript :: nodejs sqlite3 db. insert 
Javascript :: object with key as individual choice and values as the second choice 
Javascript :: add link in react table to specific column 
Javascript :: react-native-page-control 
Javascript :: JAVASCRIPT EX. 
Javascript :: How to Use the Return Keyword in a Function 
Javascript :: Function Returning This 
Javascript :: javascript add content to array 
Javascript :: metadata parser react 
Javascript :: i18next plural not working 
Javascript :: Add Methods to a Constructor Function Using Prototype 
Javascript :: js watchFile 
Javascript :: vertical lineal star pattern javascript 
Javascript :: nuxtjs update parent parameter 
Javascript :: Compiled with problems:X ERROR [eslint] Plugin "react" was conflicted between 
Javascript :: OwlCarousel not working after build react js 
Javascript :: jquery to javascript converter online free 
Javascript :: window handles 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =