I was using array like this
var names=['a','b','c'];
when I tried to access names[5]. it returns undefined simply,gives no error.
After that I changed my array like this
var names=[];
for(var i=0;i<inputNumberByUser;i++) //guys my array is populating dynamically,depends upon user input
{
names.push({FirstName:'abc',LastName:'zyx'});
}
When I tried below code, it gives me error that, Could not read FirstName of undefined
names[5].FirstName;
why above line is giving error? it should just return undefined as normal array
names[5] and names[5].FirstName both are not defined. names[5] returns 'undefined' but names[5].FirstName error. thats my point. names[5].FirstName should also simply return 'undefined' as names[5] did
Why you are getting error?
Here names[5] returns undefined and you are trying to access FirstName property of undefined element. It's like trying to get address of someone who doesn't exists.
And You also have error in line for(var i=i<3;i++) may be you wanted to write for(var i=0; i<3; i++)
Possible Solution
If you are looking to push to names array then you should do something like below:
var names=[];
// Push objects to names
for (var i = 0; i < 3; i+=1) {
names[i] = {
firstName: 'Name: ' + i
}
// Lastname defined only for 2 index object
if (i === 2) {
names[i].lastName = 'Lastname only on 2';
}
}
for (var i = 0; i < 3; i+=1) {
// outputs names[i]
console.log(names[i]);
// Check whether lastName property exists on the element or not
if (!names[i].hasOwnProperty('lastName')) {
console.log('no lastName property defined');
}
}
Here we are creating object and assigning to names[i]. Remember, i is incrementing so each time we'll be assigning objects to new position.
Then in second loop, I am just referencing to those previously assigned values and checking whether they have lastName property defined on them or not.
If you are still confused let us know and we'll try to help you.
Because your for iteration does not reach the sixth (counting zero) index, but only the third.
What you are doing inside the for (var i = 0; i < 3; i++) is essentially:
names.push({FirstName:'abc',LastName:'zyx'});
names.push({FirstName:'abc',LastName:'zyx'});
names.push({FirstName:'abc',LastName:'zyx'});
The result of the iteration would be this:
console.log(names[0].FirstName); // "abc"
console.log(names[1].FirstName); // "abc"
console.log(names[2].FirstName); // "abc"
console.log(names[3].FirstName); // undefined
console.log(names[4].FirstName); // undefined
console.log(names[5].FirstName); // undefined
By doing console.log(names[5]) you are outputting the fifth index's content of the names variable. undefined in javascript is not necessarily an error message.
names[5]
^^^ undefined
By doing console.log(names[5].FirstName) instead, you are trying to access a property of an object that does not exist, since names[5] is undefined.
names[5].FirstName
^^^ undefined
Your array has three elements. Your trying to access the sixth element which in your above code will not exist.
For the first part of your question, you have created an array having 3 elements i.e var names=['a','b','c']; and you are trying to access 6th element from your defined array names[5], which not really exist. Hence it's returning undefined.
For the second part of your question you have following codes:
var names=[];
for(var i=0;i<3;i++)
{
names.push({FirstName:'abc',LastName:'zyx'});
}
So above piece of codes create names array having 3 element and each one is an object. So you have to access it like names[0].FirstName. But you are trying to access names[5].FirstName, where 6th elements in names array is not really exist.
Hope this will help you!
Related
I'm making a program that searches through an object and prints the properties of that object to the console. The program compiles and runs, but the only thing that prints to the console is undefined. How do I fix this?
function printInfo(array) {
var usersLength = array.length;
for (var i = 0; i < usersLength; i++) {
console.log("Name: ", array.name, "Role: ", array.role);
return array.name, array.role;
}
}
This function would be passed an object with certain properties inside ( name, role, dob). The expected result is that these values would print. But what actually prints is "undefined".
Bobby
this will return undefined and an error on your console:
return array.name, array.role;
array.name property does not exist try this so you know what value function parameter has
console.log(array)
more about arrays look on this site https://www.w3schools.com/js/js_arrays.asp
If you had ES6, then use:
const printInfo = array => {
for(let info in array){
console.log(info);
}
}
this is my code:
var a = 10;
for(var i = 0; i < a; i++){ document.write(a[i]); }
I have left the var empty but it still didn't work.
a[i] looks up the property whose name is the value of i on whatever's in a. What you have in a is the primitive number 10. When you try to get an object property from a primitive number, it gets temporarily promoted to the equivalent Number instance, and then the property is taken from that instance. (Loosely speaking.)
Your i has the vaules 0 through 9, so those are the names of the properties that get looked up. Numbers don't have properties with those names, and so you get what you always get when you try to retrieve a property that doesn't exist: undefined.
You attempt to subscript an integer variable is not going to work. Try using an array instead.
var a = [item1, item2, ...];
and then use a loop to iterate over individual item and display:
for (i = 0; i < a.length; i++) {
document.write(a[i]);
}
I am trying create a function to create and assign value to an object property/member. But I'm getting the error:
Uncaught TypeError: Cannot read property 'id' of undefined
The function is suppose to create a property/member called 'id', and it is also suppose to assign a value to the newly created property/member.
var add_Ids = function(array,index) {
for(var iii = 1; iii <= 3; iii++) {
array[index+iii].id = (array[index].id * 4)+iii;
}
array[index].id *= 4;
};
array is the array that I pass into the function.
index is the position in the array to start on with the For Loop
P.S. The For Loop is not overwriting anything important.
The element that corresponds to "array[index+iii]" is empty.
array[index+iii].id = (array[index].id * 4)+iii;
In that, If array[index+iii] is undefined before, Then array[index+iii].id would give that error you've mentioned.
So before accessing id check whether the property exists.
if(!array[index+iii]) array[index+iii] = {}; // initializing if its not present.
array[index+iii].id = (array[index].id * 4)+iii;
There's a couple of things at play here.
An array element must exist before it can be manipulated. Consider this code -
var my = [];
my[0].id = 1; // TypeError: Cannot set property 'id' of undefined
This is because exactly as the error states, my[0] is undefined here. Since your array is passed into a function, do one of the following (depends upon the circumstances/requirements of this function):
Ensure that you only pass in arrays that will meet the criteria of your function. In this case, your function expects an array with 3 elements of indices [iii+index,iii+index+3].
Ensure that the count you pass in doesn't cause your operation to exceed the length of the passed in array.
Fail with a meaningful error message - test for undefined and if so, generate a meaningful error.
If this matches what you are doing, you can check for undefined and make a default option if needed.
Example of the last:
var add_ids = function(array, index) {
for (var i = 1, i <= 3; i++) {
if (!array[index + i]) {
array[index + i] = generateDefaultObject();
}
array[index + i].id = (array[index].id * 4) + i;
}
array[index].id *= 4
}
Second, it seems as though that you expect this to work. It is similar to JavaScript's implicit global declaration. For example:
my = 4; // my didn't exist before this line, but now does as a global
is (unfortunately) valid JavaScript. Arrays do not support this kind of declaration for elements - they need to exist before you work with them.
You know that in Javascript you can access the length of an text/array with length property:
var obj = ["Robert", "Smith", "John", "Mary", "Susan"];
// obj.length returns 5;
I want to know how this is implemented. Does Javascript calculates the length property when it is called? Or it is just a static property which is changed whenever the array is changed. My question is asked due to the following confusion in best-practices with javascript:
for(var i = 0; i < obj.length; i++)
{
}
My Problem: If it is a static property, then accessing the length property in each iteration is nothing to be concerned, but if it is calculated on each iteration, then it cost some memory.
I have read the following definition given by ECMAScript but it doesn't give any clue on how it is implemented. I'm afraid it might give a whole instance of array with the length property calculated in run-time, that if turns out to be true, then the above for() is dangerous to memory and instead the following should be used:
var count = obj.length;
for(var i = 0; i < count; i++)
{
}
Array in JavaScript is not a real Array type but it's an real Object type.
[].length is not being recalculated every time, it is being operated by ++ or -- operators.
See below example which is behaving same like array.length property.
var ArrayLike = {
length: 0,
push: function(val){
this[this.length] = val;
this.length++;
},
pop: function(){
delete this[this.length-1];
this.length--;
},
display: function(){
for(var i = 0; i < this.length; i++){
console.log(this[i]);
}
}
}
// output
ArrayLike.length // length == 0
ArrayLike.push('value1') // length == 1
ArrayLike.push('value2') // length == 2
ArrayLike.push('value3') // length == 3
ArrayLike.pop() // length == 2
ArrayLike.length === 2 // true
var a = ["abc","def"];
a["pqr"] = "hello";
What is a.length?
2
Why?
a.length is updated only when the index of the array is a numeric value. When you write
var a = ["abc","def"];
It is internally stored as:
a["0"] = "abc"
a["1"] = "def"
Note that the indexes are really keys which are strings.
Few more examples:
1.)
var a = ["abc","def"];
a["1001"] = "hello";
What is a.length?
1002
2.) Okay, let's try again:
var a = ["abc","def"];
a[1001] = "hello";
What is a.length?
1002
Note here, internally array is stored as
a["0"] = "abc"
a["1"] = "def"
a["1001"] = "hello"
3.) Okay, last one:
var a = ["abc"];
a["0"] = "hello";
What is a[0]?
"hello"
What is a.length?
1
It's good to know what a.length actually means: Well now you know: a.length is one more than the last numerical key present in the array.
I want to know how this is implemented. Does Javascript calculates the length property when it is called? Or it is just a static property which is changed whenever the array is changed.
Actually, your question cannot be answered in general because all the ECMA specs say is this:
The length property of this Array object is a data property whose
value is always numerically greater than the name of every deletable
property whose name is an array index.
In other words, the specs define the invariant condition of the length property, but not it's implementation. This means that different JavaScript engines could, in principle, implement different behavior.
I have a bit of Javascript where I'm POSTing my form with an XMLHttpRequest, which otherwise works fine, but when I try to add the SELECT values to the array of INPUT values, it seems Javascript has lost its mind (or I have).
This function just builds the "name1=value1&name2=value2" string and passes it along.
The code:
function sendTheForm() {
var postData = '';
var inputArr = document.getElementsByTagName('input');
if (inputArr.hasOwnProperty(length)) alert("ARRAY!!");
var selects = document.getElementsByTagName('select');
var tmpObj = new Object();
for (var i = 0; i < selects.length; i++) {
tmpObj.name = selects[i].name;
tmpObj.value = selects[i].value;
inputArr.push(tmpObj);
}
for (var i = 0; i < inputArr.length; i++) {
if (inputArr[i].value) {
if (postData.length) postData += '&';
postData += inputArr[i].name;
postData += '=';
postData += escape(inputArr[i].value);
}
}
makeHttpReq(postData)
}
The error I get in FireFox is: "inputArr.push is not a function" It does say it's an Array.
Now, besides the fact that it's a method (not a function), it seems something is going badly wrong here, it's right under my nose and I can't see it. Help?
Earlier, I tried doing:
len = inputArr.length;
inputArr[len].name = ...
and also got an error that inputArr[31] was undefined. This is probably obvious, but I'm not seeing it. Help?
Having a .length property does not mean that it is an array. getElementsByTagName() returns a DOM 2 NodeList, not an Array.
As Phrogz already said, checking for a length property is NOT a way of checking for an array.
There's only one way to do that, so it works in ALL cases.
And that is using Object.prototype.toString.call(object).slice(8, -1).
Which gives you the Class value.
Value Class Type
-------------------------------------
"foo" String string
new String("foo") String object
1.2 Number number
new Number(1.2) Number object
true Boolean boolean
new Boolean(true) Boolean object
new Date() Date object
new Error() Error object
[1,2,3] Array object
new Array(1, 2, 3) Array object
new Function("") Function function
/abc/g RegExp object (function in Nitro/V8)
new RegExp("meow") RegExp object (function in Nitro/V8)
{} Object object
new Object() Object object
The reason why this isn't working is because the inputArr variable is actually a node list, not a real array.
If you do want it to be an array, you'd loop through it and add all the nodes in it to a real array or you can do this:
var node_array = Array.prototype.slice.call(node_list);