If I console log test
I get
[
{
property_1: "a",
property_2: "b",
}
]
How can I console log the value of property_1 ?
console.log(test[0].property_1);
test is an array, who's first element is a map with keys property_1, and property_2.
test[0] accesses the first element of the array, which is a map. From there you can directly access the properties with the dot notation, or with a string subscript:
console.log(test[0]["property_1"]);
console.log(test[0]["property_1"])
First go into the array:
my_arr[0]
Then to get the property:
my_arr[0]['property_1']
End result:
var my_arr = [
{
property_1: "a",
property_2: "b",
}
]
alert(my_arr[0]['property_1']);
If that's what you get when you console.log, then I'd bet that you have a JSON string that needs to be parsed.
If so, do this...
var parsed = JSON.parse(test);
alert(parsed[0].property_1);
Related
I have a dictionary of strings to arrays of strings: {"a":["b", "c"]}. When indexing into the dictionary, I get an array of strings back, but checking if any string is in this array always returns false.
Here is the code setup:
var dict = {
"first":["a", "b", "c"]
}
if("a" in dict["first"]){
// Never hits this line
console.log("Found a!");
}
I'm pretty new to Javascript so I may be missing something about the setup of a dictionary object, but it seems pretty straightforward and I have professional experience in a few C languages so it is fairly disconcerting that this doesn't work. What is wrong here?
The in operator returns true if the specified property is in the specified object, Taken from here.
a is not a property it's an array value, array index is act as property and 0 in dict["first"] will be true here. So here you can use indexOf()
var dict = {
"first": ["a", "b", "c"]
}
if (dict["first"].indexOf("a") > -1) {
console.log("Found a!");
}
try this. this will work to you.
var dict = {
"first":["c", "b", "a"]
};
console.log(dict.first);
for( var x in dict.first){
if(dict.first[x]=="a")
{
console.log("index of a :"+ x);
break;
}
}
}
I have a requirement, where i am having a JSON object which should be converted to Key/value pairs array.
JSON:
Object {id: "1213115", transac_status: "Y", trans_id: "601427"....}
This should be converted to JS array like below:
JS Array:
var transData = [{ id: "1213115", transac_status: "Y", trans_id: 601427".... ];
I tried the below script for the conversion.
var transData = $.map(Object , function (e2, e1) {
return [[e2, e1]];
});
The array has not been converted as expected, Instead it has the following :-
Array[2]
,
Array[2]
,
Array[2]
,
Array[2]
..... etc
There is nothing wrong with your code I think. You said, that you want to produce an array with key-value pairs, which you actually do:
Array[2] , Array[2] , Array[2] , Array[2]
This is just the output that console.log produces. If you look closer at your array you'll see, that it actually is:
[["1213115", "id"], ["Y", "transac_status"], ["601427", "trans_id"]]
Thinking about it, you probably might want to switch your key/value pair, like this:
var transData = $.map(Object , function (value, key) {
return [[key, value]];
});
I renamed the function arguments to make things a little clearer.
Output would be:
[["id", "1213115"], ["transac_status", "Y"], ["trans_id, "601427"]]
Tipp: If you are working in a browser, you can just output the whole array with this line, which gives you a nice table-form output:
console.table(transData);
Is that what you are looking for? Hope that helps.
Assuming Object is a list of objects
var arr = [];
$.map(Object, function(item) {
arr.push(item);
});
This will push each object into the array;
Example
Here is my object
var myObject = {"HardGood":362,"Music":2};
console.log(myObject[0]); // undefined? instead of "Hardwood 362"
What am I doing wrong?
myObject is an object not an array, so using [0] will indeed be undefined.
Use myObject.HardGood or myObject.Music to get the value or that property
Code
console.log(myObject.HardGood); // will output 362
console.log(myObject.Music); // will output 2
UPDATE
var objects = [
{
"title": "HardGood"
"type": "362"
},
{
"title": "Music"
"type": "2"
}
];
console.log(objects[0].title); // output HardGood
console.log(objects[1].type); // output 2
You should call the first element in an object like this: myObject.key and your key is HardGood.
In arrays it's done like this:
var _Array = [];
_Array .push('x1'); //pushing in array
_Array .push('x2');
console.log(_Array[0]); // getting the first element in that array
Update: if you want to get it dynamically:
var myObject = {"HardGood":362,"Music":2};
for(var key in myObject){
console.log(key +':'+myObject[key]);
}
You have to access JSON object property with . Like below
var myObject = {"HardGood":362,"Music":2};
console.log(myObject.HardGood); //362
Useful links Have a look at below links to understand it better.
Javascript-property-access-dot-notation-vs-brackets
JS-dot-notation-vs-bracket-notation
MDN - OperatorsProperty_Accessors
I have a JSON like the following.
How can I find the length of air in console,
console.log (block.number.path[i].air.length);
{ "block": [
{ "number": "36",
"path": [
{ "air": "[{\"name\":\"0\"},{\"name\":\"1\"},{\"name\":\"2\"}]" },
{ "water": "[{\"name\":\"3\"},{\"name\":\"4\"},{\"name\":\"5\"}]" },
{ "sand": "[{\"name\":\"6\"},{\"username\":\"7\"},{\"name\":\"8\"}]" }
]
}
]
}
air itself contains a JSON encoded array, which you have to decode first:
// your obj in here
var obj = { "..." : "..." };
// grab the respective length of the "air" attribute
var air = JSON.parse( obj.block[0].path[0].air );
console.log( air.length );
http://jsfiddle.net/pLAny/
You can solve this like so:
var length = JSON.parse(block.number.path[i].air).length;
console.log(length);
It kind of looks like some of that JSON got malformed. "air", "water", and "sand" are all JSON arrays...but parsed out into strings. If you're the one generating the JSON, look into that, because it doesn't seem right. As the other answers point out, it's still solvable in Javascript using JSON.parse(), as long as you can be sure your target browsers have that interface (most modern ones).
For any JSON array (anything declared using [] brackets) you can check its .length property.
Given that json is a variable containing your JSON, you can do:
json["block"][0]["path"][0]["air"].length
Block is an array, so you have to access to the element first:
a.block[0].path[0].air.length
where a is the variable where you are holding the data.
I have a JS array:
a = ["a",["b","c"]]
How I can access string "b" in this array? Thank you very much!
You index into an array like this:
a[1][0]
Arrays are accessed using their integer indexes. Since this is an array inside an array, you use [1] to access the inner array, then get the first item in that array with [0].
That is a[1][0]
alert(a[1][0]) // "b"
As there is an alternative way also to access the element in the array which is:
a['1']['0'] //"b"
as array is internally an object so think indexes is a property of that object so
a = ["a",["b","c"]]
can be internally and object keys or property are internally transfer to string so:
a = {
'0' : "a",
'1' : ["b", "c"]
}
this also can refactor to:
a = {
'0' : "a",
'1' : {
'0' : "b",
'1' : "c"
}
}
so we can access that index as:
a['1']['0']
this will give the value as b.