How to push new elements to an array with undefined index in JavaScript - Stack Overflow

I want to create array once and then just push values to it with any index , but i get Cannot read prop

I want to create array once and then just push values to it with any index , but i get Cannot read property 'push' of undefined error

I have following scenario

var neg = [];

I want to use push randomly with any index

neg[0].push([1,2]);

or

neg[22].push([1,2]);

right now I want to define manually like neg[0] = []; , Is there any one way where i can just push to any index i want ?

I want to create array once and then just push values to it with any index , but i get Cannot read property 'push' of undefined error

I have following scenario

var neg = [];

I want to use push randomly with any index

neg[0].push([1,2]);

or

neg[22].push([1,2]);

right now I want to define manually like neg[0] = []; , Is there any one way where i can just push to any index i want ?

Share Improve this question edited Apr 22, 2019 at 20:03 ludovico 2,4361 gold badge15 silver badges35 bronze badges asked Apr 22, 2019 at 18:59 Gracie williamsGracie williams 1,1452 gold badges25 silver badges63 bronze badges 6
  • You would need to set it before you can push to it – epascarello Commented Apr 22, 2019 at 19:01
  • neg[0] is not an array, so your trying to effectively push something onto an undefined value – Derek Pollard Commented Apr 22, 2019 at 19:01
  • see this question stackoverflow./questions/31235599/… – bryan60 Commented Apr 22, 2019 at 19:02
  • Does neg have a fixed size? – Federkun Commented Apr 22, 2019 at 19:02
  • Possible duplicate of How to insert an item into an array at a specific index (JavaScript)? – Heretic Monkey Commented Apr 22, 2019 at 19:03
 |  Show 1 more ment

5 Answers 5

Reset to default 3

Here's a quick way to do exactly what you want.

var arr = [];

(arr[0]=[]).push([1,2]);

console.log(arr)


Also it's safer to check if an array already exists at this index, otherwise it will be replaced with an empty array – the above code will replace the array with an empty one if it already exists.

// creates an array at index 0 only if there's no array at this index.
// pushes an array of [1, 2] to the existing array at 0 index
// or the newly created empty one.
(arr[0] || (arr[0] = [])).push([1, 2]);;

var arr = [];

arr[0] = [1];

(arr[0]||(arr[0]=[])).push(2,3);

console.log(arr)

You are going to have to set it to an empty array before you can push it.

neg[0] = neg[0] || []
neg[0].push([1,2]);

Push will add elements to the end of an array. For example:

var neg = [];
neg.push(3); //this will add the number 3 to the end of your array

The problem here is your trying to add a value to an undefined element:

var neg = [];
neg[0].push([1,2]); // <-- neg[0] is not defined at this moment, and you're treating it as an array object.

If you define your elements as an array, you will not get such an error:

var neg = [];
neg[0] = [];
neg[22] = [];

neg[0].push([1,2]);
neg[22].push([1,2]);

Alternatively (and this is probably what you're probably looking for), you can set the value for any element of your array with the desired index:

var neg = [];

neg[0] = [1,2];
neg[22] = [1,2];

You can specify how large the array will be if you know ahead of time, for example var neg = new Array(40); will create a new array with 40 undefined elements.

Once that is created, you can change the value at any index as long as 0 <= index <= 39.

You can use array.splice(your_index, 0, your_element); to push an element into an array at the given index.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744905121a4600209.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信