草庐IT

filtered_str

全部标签

javascript - 使用 .filter 比较两个数组并返回不匹配的值

我在比较两个数组的元素和过滤掉匹配值时遇到了一些问题。我只想返回未包含在wordsToRemove中的数组元素。varfullWordList=['1','2','3','4','5'];varwordsToRemove=['1','2','3'];varfilteredKeywords=fullWordList.forEach(function(fullWordListValue){wordsToRemove.filter(function(wordsToRemoveValue){returnfullWordListValue!==wordsToRemoveValue})});con

javascript - JS : Filter object array for partial matches

是否可以过滤那些与搜索字符串匹配的对象?constarr=[{title:'Justanexample'},{title:'Anotherexam'},{title:'Somethingdifferent'}]我试过了arr.filter(x=>{returnx.title===searchStr});但这只会过滤完全匹配项,但我需要找到所有部分匹配项。letsearchStr='exam'应该给我两个对象(第一个和第二个),letsearchStr='examp'应该只给我一个对象作为结果。 最佳答案 根据您的问题,我假设您还想匹

javascript - 在 React 中使用 $splice(来自 immutability-helper)而不是 filter 从数组中删除项目有什么优势?

我正在使用immutability-helper对状态数据进行CRUD操作,想知道我是否应该始终使用$splice来删除数据,还是可以使用filter(因为它没有破坏性)?例如,假设我有一个对象数组:todos=[{id:1,body:"eat"},{id:2,body:"drink"},{id:3,body:"sleep"},{id:4,body:"run"}]给定一个项目ID,我可以通过两种方式删除它:一个。找到它的index并使用$splice:index=todos.findIndex((t)=>{return(t.id===id)});newtodos=update(todo

javascript - TypeError : str. replace is not a function strange error with vue.js Ajax 调用

我收到一个奇怪的错误:vue-resource.common.jsUncaughtTypeError:str.replaceisnotafunction它似乎与我正在获取一些的ajax调用有关数据:exportdefault{data:()=>({recipes:[]}),ready(){this.$http.get('http://localhost:3000/recipes',{headers:{'Access-Control-Allow-Origin':true}}).then((recipes)=>{this.$set('recipes',recipes)})}};我是vue.

javascript - Javascript 中的 str.charAt(5) 与 str[5]

为什么str[3]版本显然慢得多?varstr='Hello';str.charAt(3);str[3];http://jsperf.com/charat-ck编辑:对我来说,str[3]在Chrome28.0.1500.71Ubuntu13.04上慢了80%。 最佳答案 稍微调整基准:http://jsperf.com/charat-ck/4不要使用这样的常量和无操作代码,因为它很容易被删除,这样您就无法衡量您认为正在衡量的内容。接下来考虑即使我们有无限智能的JIT,这些操作也有不同的语义:当你越界调用charAt时会发生什么?只

javascript - angularjs 使用 ng-repeat 调用 Filter 两次

我是angularjs的新手,正在尝试学习过滤器的概念。但在我的例子中,angularjs调用了过滤器两次。我原以为过滤器只运行一次,但是它运行了两次。无法理解为什么会这样..这是我的plunker... 最佳答案 如果我没有误解你的问题,这与here是同一个问题。Angularjsusesa'dirty-check'approach,soitneedtocallallthefilterstoseeifexistsanychange.Afterthisitdetectthathaveachangeononevariable(theo

javascript - HAML 的 :javascript filter? 中允许使用哪种 Ruby/Erb

似乎在HAML的:javascript过滤器内部,没有Ruby代码允许,甚至没有评论。所以这是不允许的::javascript-1.upto(10)do|i|:javascript-#justacommentnottoshowtopublic(有人说没有办法在:javascript过滤器。是真的吗?但似乎唯一允许的是:javascript$('#aDiv').html('#{a_ruby_variable}');只有这个#{}是允许的。不允许使用Ruby的其他内容? 最佳答案 正如我在您的其他问题中所说,过滤器(几乎)完全与Haml

javascript - 如何使用 Array.prototype.filter 过滤对象?

给定vararr=[1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]]我们可以使用Number构造函数过滤数组arr中的数字项varres=arr.filter(Number);//[1,2,true,4,6,7,9,Array[1]]true和[10]应该在结果数组中吗?如果我们在arr处将false替换为truevararr=[1,2,false,4,{"abc":123},6,7,{"def":456},9,[10]]varres=arr.filter(Number)//[1,2,4,6,7,9,Array[1]]使用Array.isAr

javascript - 使用 jQuery .filter() 选择 div 中的特定文本

HTML:hello,mynameiskevin.what'syours?jQuery:$("p.greeting").filter(function(){return$this.text()==="mynameis";}).css("background","green");我试图在中分离出“我的名字是”这几个词标签。任何人都可以帮助我使用jQuery,因为它似乎不起作用。谢谢。 最佳答案 给你:CSS:.highlight{background-color:yellow;}JavaScript:vartext='MynamEiS

Javascript 数组 filter() 与 bind()

我正在使用filter()数组助手来遍历数组中的一些对象。我的想法是创建一个动态过滤函数以使用bind()遍历数组中的对象,但bind中的参数的使用方式与我预期的不同。这是代码:varproducts=[{name:"lettuce",type:"vegetable"},{name:"apple",type:"fruit"},{name:"carrot",type:"vegetable"},{name:"orange",type:"fruit"}];//thisisthefunctionusedinfilter()functionfilterProducts(cat,product){