Answers for "splitting string in js leads to /r"

12

javascript split by comma

<script>
var names = 'Harry,John,Clark,Peter,Rohn,Alice';
var nameArr = names.split(',');
console.log(nameArr);
 
// Accessing individual values
alert(nameArr[0]); // Outputs: Harry
alert(nameArr[1]); // Outputs: John
alert(nameArr[nameArr.length - 1]); // Outputs: Alice
 
var str = 'Hello World!';
var chars = str.split('');
console.log();
 
// Accessing individual values
alert(chars[0]); // Outputs: H
alert(chars[1]); // Outputs: e
alert(chars[chars.length - 1]); // Outputs: !
</script>
Posted by: Guest on March-31-2020
1

how to sepaarte text in object javascript

let data = 'Child 1 First Name: Ali\nChild 1 Gender: Female\nChild 1 Hair Color: Blonde\nChild 1 Hair Style: Wavy\nChild 1 Skin Tone: Tan\nChild 2 First Name: Morgan \nChild 2 Gender: Female\nChild 2 Hair Color: Brown\nChild 2 Hair Style: Ponytail\nChild 2 Skin Tone: Light\nRelationship 1 to 2: Brother\nRelationship 2 to 1: Brother\n';
let target = {};

data.split('\n').forEach((pair) => {
  if(pair !== '') {
    let splitpair = pair.split(': ');
    let key = splitpair[0].charAt(0).toLowerCase() + splitpair[0].slice(1).split(' ').join('');
    target[key] = splitpair[1];
  }
});

console.dir(target);
Posted by: Guest on May-26-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language