I am literally exhausted right now and failing to access simplest array values in javascript,
Okay, i have printed console.log(dataArray)
Console Output:
[]
account_level : "-"
address : "-"
atm_issued : "-"
avatar : "-"
branch : "-"
cnic : "-"
This is what i am trying to access by this:
console.log(dataArray['cnic'])
By this
console.log(dataArray.cnic)
And by this
console.log(dataArray[cnic])
And if i do
console.log(dataArray.cnic)
It prints undefined
console.log(dataArray.length, dataArray instanceof Array, dataArray instanceof Object);
Output of above:
0 true true
How do I access this property?
6 Answers 6
In your case you can't define an array like this beacause the syntax is not compatible.
I suggest you instead of this to use OBJECT SYNTAX as the following code snippet shows:
var obj={
account_level : "DATA",
address : "-",
atm_issued : "-",
avatar : "-",
branch : "-",
cnic : "-"
};
console.log(obj.account_level);
You can Also fill the object dynamically like this:
var obj={};
obj["address"] = "DATA1";
obj["atm_issued"] = "DATA2";
obj["avatar"] = "DATA3";
obj["branch"] = "DATA4";
obj["cnic"] = "DATA5";
console.log(obj.cnic);
console.log(obj);
5 Comments
var arr = []; arr.cnic = 'foo';. The OP should not use an array for their purposes, but it's possible.The syntax you are using is how you would access an object.
let object = {
account_level: "-"
address: "-"
atm_issued: "-"
avatar: "-"
branch: "-"
cnic: "-"
};
console.log(object.cnic);
To access an array you use its index.
let array = ['Chelsea', 'Liverpool', 'Arsenal'];
console.log(array[0]);
5 Comments
cnic, then using array.cnic will work perfectly fine. After all, accessing array.length works fine too.If an array has a property cnic, then using array.cnic... in the example I provide if I do array.Chelsea ir returns undefined. How can an array have a property? Im confused :-Svar arr = []; arr.Chelsea = 'foo';. In your example, array does not have a Chelsea property, it has the string value 'Chelsea' as one of it's elements. That's different.JS arrays always have integers as index. If your key is a text, that means it is an object.
you can access an object these ways
var a = {key1 : "value1", key2 : "value2"};
//METHOD 1
console.log(a.key1);
//METHOD 2
console.log(a['key1']);
This is array
var a = ["value1","value2"];
console.log(a[0]);
2 Comments
What you have specify is something like object not an array. In case of object the key:value pair are separated by , which is missing in your format. So the final format is like:
var obj = { "account_level" : "-",
"address" : "-",
"atm_issued" : "-",
"avatar" : "-",
"branch" : "-",
"cnic" : "-" };
console.log(obj.cnic);
1 Comment
It looks like something weird is going on with the way your array/object is being filled, please try creating a new one from it as follows :
dataArray = Object.assign({}, dataArray);
console.log(console.log(dataArray.cnic));
8 Comments
dataArray doesn't have such a property, how does copying a non-existing property make it appear?dataArray.cnic, then won't be possible to access it after copying it.Object.assign but my reasoning was that maybe it would resolve this kind of weird glitch he is facing. Ok sure he cannot access the property directly but the console still sees it.dataArray.cnic as well. Try this in the console: var obj1 = {}; console.log(obj1); var obj2 = Object.assign({}, obj1); obj1.foo = 42; console.log(obj2);. Notice how expanding obj1 shows the property (even though it could not have possibly existed at the time we logged the object) and obj2 does not.It was a pain, but i was using firebase, and accessing properties before they were being populated. So first i have let them be populated then access them.
console.log(dataArray.length, dataArray instanceof Array, dataArray instanceof Object);console.log(dataArray.__proto__);.