Example 1: javascript explode
//split into array of strings.
var str = "Well, how, are , we , doing, today";
var res = str.split(",");
Example 2: 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);
Example 3: convert string to array js
// créer une instance d'Array à partir de l'objet arguments qui est semblable à un tableau
function f() {
return Array.from(arguments);
}
f(1, 2, 3);
// [1, 2, 3]
// Ça fonctionne avec tous les objets itérables...
// Set
const s = new Set(["toto", "truc", "truc", "bidule"]);
Array.from(s);
// ["toto", "truc", "bidule"]
// Map
const m = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(m);
// [[1, 2], [2, 4], [4, 8]]
const mapper = new Map([["1", "a"], ["2", "b"]]);
Array.from(mapper.values());
// ["a", "b"]
Array.from(mapper.keys());
// ["1", "2"]
// String
Array.from("toto");
// ["t", "o", "t", "o"]
// En utilisant une fonction fléchée pour remplacer map
// et manipuler des éléments
Array.from([1, 2, 3], x => x + x);
// [2, 4, 6]
// Pour générer une séquence de nombres
Array.from({length: 5}, (v, k) => k);
// [0, 1, 2, 3, 4]
Example 4: split() javascript
// It essentially SPLITS the sentence inserted in the required argument
// into an array of words that make up the sentence.
var input = "How are you doing today?";
var result = input.split(" "); // Code piece by ZioTino
// the following code would return as
// string["How", "are", "you", "doing", "todaY"];