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
我已经寻找解决方案,但没有找到。 我有以下字符串。 1. hello 2. HELLO 3. hello_world 4. HELLO_WORLD 5. Hello World 我想将它们转换为以下内容: 1. Hello 2. Hello 3. HelloWorld 4. HelloWorld 5. HelloWorld 如果字符串中没有空格和下划线,则首先使用大写字母,而其他字母使用小写字母。 如果单词由下划线或空格分隔,则每个单词的大写首字母并删除空格和下划线。 我怎样才能在JavaScript中做到这一点。 谢谢
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
我有一个JSON obj,经过一些操作(如删除一些部分)后,我将其打印出来,除了我有一些null值之外,一切看起来都不错。 我如何删除这些? 我使用JSON.stringify(obj, null, 2)方法进行打印,以下是它的样子: { "store": { "book": [ null, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour",
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
我正在寻找一个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
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中删除数组中的空元素36个答案 只需使用.filter(Boolean)以仅保留所需的项目。 var str = 'C3B1A60'; var splits = 'C3B1A60'.split(/([A-Z]d+)/).filter(Boolean); console.log(splits);
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)
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); } } }
这个问题在这里已经有了答案: 在Javascript中删除数组中的空元素36个答案 问题是过滤器返回一个新的数组。 为什么不使用for循环和拼接: if (Array.isArray(object)) { for (var i = object.length - 1; i >= 0; i--) { if (object[i] === undefined) { object.splice(i, 1); } } }
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']
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中删除数组中的空元素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)
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 }
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) {