2

It's simple, i want to add an undefined element to an array, lets say i have an array Punkt and i have punkt[0] = x: 15, y:"16s" . As i know for ex. the element will have 4 punkt in total i want to add an undefined element to punkt[3], so that also the others punkt[1] and punkt[2] will become undefined. I need this because somewhere later in my code i do a check for undefined, and throw an error based on this.

Racks is an object that contains an array of objects Punkt

punkt[0] = {
x: 5,
y: "16s" }

What i tried:

racks[trimdevID].punkt[$('#posTable tr:last td:first').text()] = undefined;
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
Faarbhurtz
  • 550
  • 1
  • 8
  • 27
  • Well what happens when you try that? Do you get an error? What's the value of that jQuery `.text()` expression? – Pointy Dec 03 '13 at 13:30
  • Why are you prefilling the array? If you try to access `punkt[1]` it will return `undefined`. What benefit is there of putting `undefined` in there? – Matt Ellen Dec 03 '13 at 13:30
  • Are you simply trying to empty your array? You shouldn't really ever want to make an array index undefined, you should simply remove that particular index using splice. – Michael Tempest Dec 03 '13 at 13:31
  • Nothing happens at the moment :( the .text will return a value that is a number, possibly in a string, i will try casting that – Faarbhurtz Dec 03 '13 at 13:33
  • *"i want to add an `undefined` element to `punkt[3]`, so that also the others `punkt[1]` and `punkt[2]` will become `undefined`"* Why? The default value is `undefined`. Adding `punkt[3]` doesn't really do anything for you. It certainly does nothing to change `punkt[1]` and `punkt[2]`. – Blue Skies Dec 03 '13 at 13:39
  • You're welcome. FYI, if you want your Array to show a `.length` of `4`, then setting `punkt[3]` would do that, but you can also just do it manually. `punkt.length = 4;` – Blue Skies Dec 03 '13 at 13:42

3 Answers3

2

undefined is built in property of the global object and it's indeed what you need, so such code for example is working:

var arr = [];
arr.push(undefined);
alert(typeof arr[0]);

This means your problem is elsewhere, from quick look it's because you're using non integer index. Try this instead and it should work:

var index = parseInt($('#posTable tr:last td:first').text(), 10);
if (!isNaN(index))
    racks[trimdevID].punkt[index] = undefined;
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
2

You can certainly use the push() routine (see http://www.w3schools.com/jsref/jsref_push.asp). In your example, that would look something like:

// push two undefined elements:
racks[trimdevID].punkt.push(undefined)
racks[trimdevID].punkt.push(undefined)

That assumes that you already have exactly two elements in the array and you want exactly four elements. The more generic version of this is:

var newLength = 4;
for (var idx = racks[trimdevID].punkt.length; idx <= newLength; idx++)
{
   racks[trimdevID].punkt.push(undefined);
}

I believe other similar stack questions also have the same answer. You might look at this question for more examples (How to initialize an array's length in javascript?)

Community
  • 1
  • 1
drew_w
  • 10,320
  • 4
  • 28
  • 49
1

You can try .push in array

yourArray.push(undefined)
i = 0; //Position Of Undefined
console.log(typeof yourArray[i])
Prateek
  • 6,785
  • 2
  • 24
  • 37