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 bee 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;
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 bee 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;
Share
Improve this question
edited Dec 3, 2013 at 13:41
Abdennour TOUMI
93.7k42 gold badges268 silver badges269 bronze badges
asked Dec 3, 2013 at 13:27
FaarbhurtzFaarbhurtz
5801 gold badge8 silver badges27 bronze badges
6
-
Well what happens when you try that? Do you get an error? What's the value of that jQuery
.text()
expression? – Pointy Commented Dec 3, 2013 at 13:30 -
Why are you prefilling the array? If you try to access
punkt[1]
it will returnundefined
. What benefit is there of puttingundefined
in there? – Matt Ellen Commented Dec 3, 2013 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 Commented Dec 3, 2013 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 Commented Dec 3, 2013 at 13:33
-
"i want to add an
undefined
element topunkt[3]
, so that also the otherspunkt[1]
andpunkt[2]
will beeundefined
" Why? The default value isundefined
. Addingpunkt[3]
doesn't really do anything for you. It certainly does nothing to changepunkt[1]
andpunkt[2]
. – Blue Skies Commented Dec 3, 2013 at 13:39
3 Answers
Reset to default 2You can certainly use the push() routine (see http://www.w3schools./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?)
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;
You can try .push in array
yourArray.push(undefined)
i = 0; //Position Of Undefined
console.log(typeof yourArray[i])