underscore.js

使用JavaScript从数组中删除对象

我只是一个虾纸丫 提交于 2020-02-27 04:25:20
如何从数组中删除对象? 我希望从 someArray 删除包含名称 Kristian 的对象。 例如: someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}]; 我要实现: someArray = [{name:"John", lines:"1,19,26,96"}]; #1楼 在数组上使用拼接功能。 指定起始元素的位置和要删除的子序列的长度。 someArray.splice(pos, 1); #2楼 您可以使用多种方法从数组中删除项目: //1 someArray.shift(); // first element removed //2 someArray = someArray.slice(1); // first element removed //3 someArray.splice(0, 1); // first element removed //4 someArray.pop(); // last element removed //5 someArray = someArray.slice(0, a.length - 1); // last element removed //6 someArray.length = someArray.length -

用户完成键入而不是按键盘键时运行javascript函数?

人盡茶涼 提交于 2020-02-26 21:38:26
我想在用户完成在文本框中的输入后触发ajax请求。 我不希望它在用户每次输入字母时都运行该函数,因为这会导致很多ajax请求,但是我也不希望他们也必须按下Enter键。 有没有一种方法可以让我检测用户何时完成键入,然后再执行ajax请求? 在这里使用jQuery! 戴夫 #1楼 如果您要查找特定长度(例如邮政编码字段): $("input").live("keyup", function( event ){ if(this.value.length == this.getAttribute('maxlength')) { //make ajax request here after. } }); #2楼 var timer; var timeout = 1000; $('#in').keyup(function(){ clearTimeout(timer); if ($('#in').val) { timer = setTimeout(function(){ //do stuff here e.g ajax call etc.... var v = $("#in").val(); $("#out").html(v); }, timeout); } }); 此处的完整示例: http : //jsfiddle.net/ZYXp4/8/ #3楼 不知道我的需求是否有点奇怪

如何使用jQuery从数组中删除特定值

北城以北 提交于 2020-02-25 18:58:19
我有一个看起来像这样的数组: var y = [1, 2, 3]; 我想从数组 y 删除 2 。 如何使用jQuery从数组中删除特定值? 我尝试过 pop() 但是总是删除最后一个元素。 #1楼 //This prototype function allows you to remove even array from array Array.prototype.remove = function(x) { var i; for(i in this){ if(this[i].toString() == x.toString()){ this.splice(i,1) } } } 使用例 var arr = [1,2,[1,1], 'abc']; arr.remove([1,1]); console.log(arr) //[1, 2, 'abc'] var arr = [1,2,[1,1], 'abc']; arr.remove(1); console.log(arr) //[2, [1,1], 'abc'] var arr = [1,2,[1,1], 'abc']; arr.remove('abc'); console.log(arr) //[1, 2, [1,1]] 要使用此原型函数,您需要将其粘贴到代码中。 然后,您可以将其应用于任何带有“点表示法”的数组: someArr

custom BackboneJS events(increment and Decrement) not working

吃可爱长大的小学妹 提交于 2020-02-07 04:39:48
问题 I am having a counter(No. of products) that I want to manipulate using backboneJS custom events. If I click Add Product then No. of products should increase by and if I click Remove Product then No. of products should decrease by one.Demo here The problem is, value of counter is not getting updated when i click buttons. here is code snippet var Counter = Backbone.Model.extend({ defaults: { value: 10 }, // model methods increment: function() { this.set({value: this.get('value')+1}); },

How to test _.defer() using Jasmine, AngularJs

泄露秘密 提交于 2020-01-30 03:11:53
问题 I already asked this question where the main point was scope doesn't exists in terminal but it exists in Chrome debugging tool. Despite the answers it didn't get fixed. The question is what is the right syntax to test the bellow directive, especially the line expect(scope.measurementScroll).toBe(true); . While digging through web i couldn't find any similar question most questions are related to $q.defer() where in my case there is underscore method _.defer() Controller 'use strict'; angular

Javascript: How to merge the string values of two arrays?

a 夏天 提交于 2020-01-26 04:54:22
问题 I want to use the _.zip function of Underscore.js to create pairs from two arrays. var a = ["alpha", "beta", "gamma"]; var b = ["one", "two", "three"]; var pairs = _.zip(a, b); alert("pairs = " + pairs); This seems to work fine when done with integers however when using strings the result is confusing: pairs = 3,3,3,3,3,3 Instead I expect the following result: pairs = [["alpha", "one"], ["beta", "two"], ["gamma","three"]] Update: Thanks to the comments I discovered that the described behavior

How to use after and each in conjunction to create a synchronous loop in underscore js

微笑、不失礼 提交于 2020-01-25 02:57:09
问题 Hi I'm trying to create a synchronous loop using underscore js. For each loop iteration I make some further asynchronous calls. However, I need to wait until each iteration call is finished before I move on to the next iteration. Is this possible in underscore js ? If yes, how so ? could someone please provide an example ? _.( items ).each( function(item) { // make aync call and wait till done processItem(item, function callBack(data, err){ // success. ready to move to the next item. }); //

Searching for text inside nested object (Backbone.js collection as example)

人盡茶涼 提交于 2020-01-22 15:42:26
问题 I have a backbone.js collection where I need to do a fulltextsearch on. The tools I have at hand are the following: Backbone.js, underscore.js, jQuery For those not familiar with backbone: A backbones collection is just an object. Inside the collection there is an array with models. Each model has an array with attributes. I have to search each attribute for a string. The code I am using for this is: query = 'some user input'; query = $.trim(query); query = query.replace(/ /gi, '|'); var

Update if exists or add new element to array of objects - elegant way in javascript + lodash

三世轮回 提交于 2020-01-19 12:17:27
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description:

Update if exists or add new element to array of objects - elegant way in javascript + lodash

谁说我不能喝 提交于 2020-01-19 12:17:24
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description: