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;
6
  • Well what happens when you try that? Do you get an error? What's the value of that jQuery .text() expression? Commented Dec 3, 2013 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? 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. 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 Commented Dec 3, 2013 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]. Commented Dec 3, 2013 at 13:39

3 Answers 3

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?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link, initializing array lenght also helps for me
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;

3 Comments

if(changingPos<parseInt($('#posTable tr:last td:first').text())) { racks[trimdevID].punkt[parseInt($('#posTable tr:last td:first').text())] = undefined; }
undefined isn't a keyword.
@BlueSkies wrong semantics... not a keyword and you can declare local variable with same name but it's still what I meant. Semantics fixed. :)
1

You can try .push in array

yourArray.push(undefined)
i = 0; //Position Of Undefined
console.log(typeof yourArray[i])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.