splice

Splice doesn't copy array of objects [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: How do you clone an Array of Objects in Javascript? 29 answers using splice(0) to duplicate arrays 3 answers In this plunk I have an example of an array of objects a that I copy with slice() to b . I alter one of the objects in a but it changes also b . Isn't slice supposed to copy the array, including its contents? I need a and b to have different pointers. Javascript var a = [{x1:1, x2:2}, {x1:3, x2:4}]; var b = a.slice(); a[1].x1 = 5; console.log(b[1]); this prints: x1: 5 x2: 4 回答1: From MDN: For

Javascript TypeError ____ is not a function

匿名 (未验证) 提交于 2019-12-03 02:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am currently using the Chrome console to do some debugging for a Greasemonkey script. From the console I run var opp = document.querySelectorAll('a[class="F-reset"]'); and it works as expected. But if I try to remove the first element with opp.splice(0,1) I get the following error Uncaught TypeError: opp.splice is not a function at <anonymous>:2:5 at Object.InjectedScript._evaluateOn (<anonymous>:905:140) at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34) at Object.InjectedScript.evaluate (<anonymous>:694:21) If I try to run

Why am I getting this output from this simple splice command?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a small issue with understanding why I'm getting this output. var arr = ["a", "b", "c", "d", "e", "f"]; arr.splice(2,0,"1"); console.log(arr); var arr2 = ["a", "b", "c", "d", "e", "f"]; arr2 = arr2.splice(2,0,"2"); console.log(arr2); ouput is: [ 'a', 'b', '1', 'c', 'd', 'e', 'f' ] [] Why is the second line of output not: [ 'a', 'b', '2', 'c', 'd', 'e', 'f' ] Is it an issue of assignment or what? 回答1: Reading about splice method. It returns An array containing the removed elements. If only one element is removed, an array of one

Whats the best way to update an object in an array in ReactJS?

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If you have an array as part of your state, and that array contains objects, whats an easy way to update the state with a change to one of those objects? Example, modified from the tutorial on react: var CommentBox = React.createClass({ getInitialState: function() { return {data: [ { id: 1, author: "john", text: "foo" }, { id: 2, author: "bob", text: "bar" } ]}; }, handleCommentEdit: function(id, text) { var existingComment = this.state.data.filter({ function(c) { c.id == id; }).first(); var updatedComments = ??; // not sure how to do this

Javascript swap array elements

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any simpler way to swap two elements in an array? var a = list[x], b = list[y]; list[y] = a; list[x] = b; 回答1: You only need one temporary variable. var b = list[y]; list[y] = list[x]; list[x] = b; 回答2: If you want a single expression, using native javascript, remember that the return value from a splice operation contains the element(s) that was removed. var A = [1, 2, 3, 4, 5, 6, 7, 8, 9], x= 0, y= 1; A[x] = A.splice(y, 1, A[x])[0]; alert(A); // alerts "2,1,3,4,5,6,7,8,9" Edit: The [0] is necessary at the end of the expression as

How to remove element from an array in JavaScript?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: var arr = [ 1 , 2 , 3 , 5 , 6 ]; I want to remove the 1st element of the array so that it becomes: var arr = [ 2 , 3 , 5 , 6 ]; To extend this question, what if I want to remove the 2nd element of the array so that it becomes: var arr = [ 1 , 3 , 5 , 6 ]; 回答1: For a more flexible solution, use the splice() function. It allows you to remove any item in an Array based on Index Value: var indexToRemove = 0 ; var numberToRemove = 1 ; arr . splice ( indexToRemove , numberToRemove ); 回答2: shift() is ideal for your situation. shift()

How to delete/unset the properties of a javascript object? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Possible Duplicates: How to unset a Javascript variable? How to remove a property from a javascript object I'm looking for a way to remove/unset the properties of a JS object so they'll no longer come up if I loop through the object doing for (var i in myObject) . How can this be done? 回答1: simply use delete , but be aware that you should read fully what the effects are of using this: delete object.index; //true object.index; //undefined but if I was to use like so: var x = 1; //1 delete x; //false x; //1 but if you do wish to delete

splice() not updating items order of array within knockout.js

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Following a prior post on how to update the order of an array. I followed the suggestion of Michael Best and used splice() to modify the ordering of my array on button click self . moveup = function ( itemIndex ) { var i = self . itemList . indexOf ( itemIndex ); if ( i >= 1 ){ var array = self . itemList (); self . itemList . splice ( i - 1 , 2 , array [ i ], array [ i - 1 ]); } Where I am having trouble is in incrementing the items in the array. From reading the usage of Array Splice The first param indicates where the change

javascript splice() causes “arrayName.splice() is not a function” error

匿名 (未验证) 提交于 2019-12-03 00:54:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to remove certain values from an array containing input fields in a form: allFields = theForm.getElementsByTagName("INPUT"); for(j = 0; j < allFields.length; j++) { if(allFields[j].className == "btn" || allFields[j].className == "lnk") { allFields.splice(j,1); } } It causes an error. Firebug shows following error and the script doesn't work. allFields.splice is not a function This also happened with any other Array method I tried. How can I fix this? 回答1: allFields is not an array, but a NodeList . If you want to remove elements,

Stack vs. Heap in Javascript? (Maximum call stack size exceeded)

匿名 (未验证) 提交于 2019-12-03 00:48:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to build a web-page for which I need to shovel around several 100MB of data in JavaScript. With different browsers I run into "maximum call stack size exceeded" errors at different data amounts. Can I fix this issue by going through my code and trying to move local variables inside functions into a more global scope to try to get them to be allocated on the heap instead of the stack? Or do these concepts not exist in JavaScript? (As far as I know, I don't have any major recursive loops in my data, so it really is a couple of huge