草庐IT

array_replace

全部标签

javascript - Array.prototype.slice 奇怪的行为

考虑这段代码,每行末尾都有控制台输出:functionwhatever(){console.log(arguments)//{'0':1,'1':2,'2':3,'3':4,'4':5}console.log(Array.prototype.slice.call(arguments))//[1,2,3,4,5]console.log(Array.prototype.slice.call({'0':1,'1':2,'2':3,'3':4,'4':5}))//[]}whatever(1,2,3,4,5)为什么第三个console.log输出一个空数组? 最佳答案

javascript - Array.filter 无法正常工作

我有一个数组,我想从中删除一条记录我已经使用了Array.filter()但它返回的是相同的数组。我的代码:varurl=window.location.pathname,orderId=url.split('/').slice(-2)[0];varCart=JSON.parse(localStorage.getItem('Cart'));newCart=Cart.filter(function(item){if(parseInt(item.orderId)==parseInt(orderId)){return{};}else{returnitem;}});localStorage.s

javascript - Prototype 在这个 Array slice 调用中,为什么?

我正在阅读JS函数的arguments变量的MDN页面:https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments我知道arguments不是数组,所以这行不通:vara=arguments.slice();MDN上的解决方案是这样做:varargs=Array.prototype.slice.call(arguments);为什么使用Array.prototype而不仅仅是Array.slice.call(arguments)?在这里使用原型(prototyp

javascript - 在 IE 中将函数添加到 Array.prototype 会导致它作为元素被插入每个数组

我在项目的开头将以下polyfill添加到Array:if(!Array.prototype.find){Array.prototype.find=function(predicate){if(this===null){thrownewTypeError('Array.prototype.findcalledonnullorundefined');}if(typeofpredicate!=='function'){thrownewTypeError('predicatemustbeafunction');}varlist=Object(this);varlength=list.leng

javascript - 你能用用户定义的对象伪造 Array.isArray() 吗?

我很好奇是否有任何方法可以用用户定义的对象伪造出Array.isArray()。摘自《JavaScript模式》一书:Array.isArray([]);//true//tryingtofoolthecheck//withanarray-likeobjectArray.isArray({length:1,"0":1,slice:function(){}});//false那个对象显然失败了,但是还有其他方法吗?这纯粹是出于好奇,并不是因为我认为您可以在常规客户端代码中搞砸.isArray()(尽管知道如果可以的话显然会很棒!)。 最佳答案

javascript - TypeScript/JavaScript 中的 array.indexOf

这个问题在这里已经有了答案:indexOfmethodinanobjectarray?(29个答案)关闭6年前。更新:虽然这个问题被标记为与this重复.但是@ssube的方法很简洁,也更聪明。更新2:@Grungondola的评论中似乎有新的方法可以做到这一点。我正在使用Typescript。这很有效。vararray1=[];array1.push(5);array1.push(6);console.log("a",array2.indexOf(6));但这并不能很好地工作。因为array2.indexOf返回-1,这意味着它没有找到它。vararray2=[];array2.pu

JavaScript 1.6 Array.map() 和 Array.filter() 不使用内置函数作为参数

这很好用:["655971","2343","343"].map(function(x){returnparseInt(x)})//[655971,2343,343]但这不是:["655971","2343","343"].map(parseInt)//[655971,NaN,NaN]Array.filter()也是如此我在这里错过了什么? 最佳答案 这是因为map向回调函数传递的参数不仅仅是数组项。你得到:callback(item,index,array)通常你的函数会忽略它不需要的参数。但是parseInt接受一个可选的第二个

javascript - 对于这种特定情况 : replace .,如何使用 JavaScript 替换字符串中的所有字符 by _

JavaScript中的以下语句按预期工作:vars1=s2.replace(//gi,'_');//replaceallspacesbythecharacter_但是,要替换所有出现的字符.通过字符_,我有:vars1=s2.replace(/./gi,'_');但结果是一个完全由字符_填充的字符串为什么以及如何替换.通过_使用JavaScript? 最佳答案 .正则表达式中的字符将匹配所有内容。你需要转义它,因为你想要一个文字句点字符:vars1=s2.replace(/\./gi,'_');

javascript - 如何在 JavaScript 中使用 array reduce with condition?

所以我有一个数组constrecords=[{value:24,gender:"BOYS"},{value:42,gender:"BOYS"},{value:85,gender:"GIRLS"},{value:12,gender:"GIRLS"},{value:10,gender:"BOYS"}]我想得到sum所以我使用了JavaScriptarrayreduce函数并得到了它。这是我的代码:someFunction(){returnrecords.reduce(function(sum,record){returnsum+record.value;},0);}通过该代码,我得到了正确

javascript - 在 Jasmine 中,array.includes 不起作用(必须用其他函数代替)。为什么?

我的问题看起来很奇怪。我有一个带有一个新的、非常简单的函数的构造函数,它应该检查一个变量是否包含在一个数组中。它工作得很好(我在一个表单中使用这个函数)。但是...我无法对此函数编写任何单元测试,因为Karma/Jasmine看不到数组的“包含”函数。有人可以建议我该怎么做吗?这里的情况稍微简化了一点://要测试的构造函数vm.isNameAlreadyUsed=function(){//debutlogging:console.log("vm.allNames",vm.allNames);//output:vm.allNames['A','B','C']console.log("an