How to make first character uppercase of all words in JavaScript?

I have searched for solution but did not find yet. I have the following string. 1. hello 2. HELLO 3. hello_world 4. HELLO_WORLD 5. Hello World I want to convert them to following: 1. Hello 2. Hello 3. HelloWorld 4. HelloWorld 5. HelloWorld If there is No space and underscore in string just uppercase first and all others to lowercase. If words are separated by underscore or space then Upper

如何使JavaScript中的所有单词的第一个字符大写?

我已经寻找解决方案,但没有找到。 我有以下字符串。 1. hello 2. HELLO 3. hello_world 4. HELLO_WORLD 5. Hello World 我想将它们转换为以下内容: 1. Hello 2. Hello 3. HelloWorld 4. HelloWorld 5. HelloWorld 如果字符串中没有空格和下划线,则首先使用大写字母,而其他字母使用小写字母。 如果单词由下划线或空格分隔,则每个单词的大写首字母并删除空格和下划线。 我怎样才能在JavaScript中做到这一点。 谢谢

Recursively remove null values from JavaScript object

I have a JSON obj, after some operations (like delete some pieces), I print it and everything looks good except that I have some null values. How do I remove these? I use JSON.stringify(obj, null, 2) method to print, and here is what it looks like: { "store": { "book": [ null, { "category": "fiction", "author": "Evelyn Wa

递归地从JavaScript对象中删除空值

我有一个JSON obj,经过一些操作(如删除一些部分)后,我将其打印出来,除了我有一些null值之外,一切看起来都不错。 我如何删除这些? 我使用JSON.stringify(obj, null, 2)方法进行打印,以下是它的样子: { "store": { "book": [ null, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour",

what is Array.any? for javascript

I'm looking for a method for javascript returns true or false when it's empty.. something like Ruby any? or empty? [].any? #=> false [].empty? #=> true Javascript standard library is quite poor compared to ruby's Enumerable, most stuff should be written by hand. On the bright side, this is trivial in most cases. Array.prototype.isEmpty = function() { return !this.leng

什么是Array.any? 为JavaScript

我正在寻找一个JavaScript的方法返回true或false时,它是空的..像Ruby的东西any? 还是empty? [].any? #=> false [].empty? #=> true 与ruby的Enumerable相比,Javascript标准库相当差,大部分内容都应该由人工编写。 光明的一面,在大多数情况下这是微不足道的。 Array.prototype.isEmpty = function() { return !this.length; } Array.prototype.any = function(func) { return this.some(func || funct

JavaScript: splitting letters followed by digits

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers 只需使用.filter(Boolean)以仅保留所需的项目。 var str = 'C3B1A60'; var splits = 'C3B1A60'.split(/([A-Z]d+)/).filter(Boolean); console.log(splits);

JavaScript:分割字母后跟数字

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 只需使用.filter(Boolean)以仅保留所需的项目。 var str = 'C3B1A60'; var splits = 'C3B1A60'.split(/([A-Z]d+)/).filter(Boolean); console.log(splits);

Removing empty or undefined elements from an array

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers 这是我过去写的一个有用的代码片段。 Array.prototype.empty = function() { for (var i = 0, s = this.length; i < s; i++) { this.pop(); } return this; }; Array.prototype.removeAll = function(item) { var result = []; for (var i = 0, j = 0, s = this.length; i < s; i++

从数组中删除空或未定义的元素

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 这是我过去写的一个有用的代码片段。 Array.prototype.empty = function() { for (var i = 0, s = this.length; i < s; i++) { this.pop(); } return this; }; Array.prototype.removeAll = function(item) { var result = []; for (var i = 0, j = 0, s = this.length; i < s; i++) { if (this[i] != item)

.pull` to vanilla JS?

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers The problem is that filter returns a new array. Why not just use a for loop and splice: if (Array.isArray(object)) { for (var i = object.length - 1; i >= 0; i--) { if (object[i] === undefined) { object.splice(i, 1); } } }

.pull`给香草JS?

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 问题是过滤器返回一个新的数组。 为什么不使用for循环和拼接: if (Array.isArray(object)) { for (var i = object.length - 1; i >= 0; i--) { if (object[i] === undefined) { object.splice(i, 1); } } }

Distinct array of variable

This question already has an answer here: Get all unique values in an array (remove duplicates) 57 answers Remove empty elements from an array in Javascript 36 answers This might help (Unique values in an array). This answer is from above link function onlyUnique(value, index, self) { return self.indexOf(value) === index; } usage example: var a = ['a', 1, 'a', 2, '1']; var unique =

不同的变量数组

这个问题在这里已经有了答案: 获取数组中的所有唯一值(删除重复项)57个答案 在Javascript中删除数组中的空元素36个答案 这可能有帮助(数组中的唯一值)。 这个答案是从上面的链接 function onlyUnique(value, index, self) { return self.indexOf(value) === index; } 用法示例: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']

Deleting undefined indexes in array Javascript

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers var array = []; array[1] = 2; array[3] = 4; for(var i = 0; i < array.length; i ++) { if(array[i]) { } else { array.splice(i, 1); } } console.log(array)

删除数组Javascript中的未定义索引

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 var array = []; array[1] = 2; array[3] = 4; for(var i = 0; i < array.length; i ++) { if(array[i]) { } else { array.splice(i, 1); } } console.log(array)

Removing an empty string from an array logic

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers One problem is that Arrays and Objects are slightly different things. To iterate over an array you can use things like: for (var i=0; i<arr.length; i++) { /* code */ } arr.forEach(function(item, index, arr) { /* code */ }); The for..in structure is used to iterate over the keys in

从数组逻辑中删除空字符串

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 一个问题是数组和对象是稍有不同的东西。 要遍历数组,你可以使用如下的东西: for (var i=0; i<arr.length; i++) { /* code */ } arr.forEach(function(item, index, arr) { /* code */ }); for..in结构用于迭代对象中的键: var obj = { b : 1, c:2 } for (var key in obj) { console.log(key) // will output first b then c }

How can I "reset" an array's keys?

This question already has an answer here: Remove empty elements from an array in Javascript 36 answers For example, you can use Array.prototype.filter method. var array = []; array[30] = 1; array.length; // 31 var compactArray = array.filter(function (item) { return item !== undefined; }); compactArray.length; // 1 If it's an object, for..in loop will be usefull var array = { 31:

我怎样才能“重置”一个数组的键?

这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 例如,你可以使用Array.prototype.filter方法。 var array = []; array[30] = 1; array.length; // 31 var compactArray = array.filter(function (item) { return item !== undefined; }); compactArray.length; // 1 如果它是一个对象, for..in循环将是有用的 var array = { 31: 1}; var compactArray = []; for (var i in array) {