cannot print component in array in javascript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 months ago.
i'm just a newbie on javascript , please help me this
var arr = [{"student-name":"Harry Potter","discipline":"Magic"}] ;
how can i get "Harry Potter" in this array , thanks for helping

Related

Read from JSON where the name is [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
My resultjson's content:
[{"name":"Dummy","id":"780828872962080819","sync_code":123456,"expiration":"2021-04-07T14:03:34.000Z"}]
I need the "name" from the JSON as a String, i tried with JSON.parse(resultjson).name but it didnt work.
It's an array. You need to access an element:
JSON.parse(resultjson)[0 /* or other element */].name

How to loop through json response in javascript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Loop through an array in JavaScript
(46 answers)
JavaScript loop through JSON array?
(14 answers)
Closed 2 years ago.
Note: I have already gone through stackoverflow similar questions but without any luck.
I have following json response received in ajax. I am trying to loop through it but still no success.
[
{"id":"1","type":"size","value":"large","datetime":"2020-10-20 13:45:49"},
{"id":"14","type":"color","value":"red","datetime":"2020-10-20 13:45:49"}
]
I tried many ways including following code but could not get desired result.
for( let prop in responce ){
console.log( responce[prop].id );
}
for( let prop in responce ){
console.log( responce[prop] );
}
Kindly advise how to resolve it?

JavaScript hash of Arrays [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 years ago.
I have such type of array
How to access Viena
There could be a lot of cities
[{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"},{..},{..}],{"Paris":[{..}]} ]
You can use Array.filter for this.
var data = [{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]},{"Paris":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]}];
var result = data.filter(function(value){return Object.keys(value).indexOf("Viena") != -1;});
console.log(result[0]);

How do we find the length of an object in Javascript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!
Just like that:
Object.keys(objectName).length;

Javascript, convert a list of objects into an array? [duplicate]

This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
Closed 6 years ago.
I have this:
How do I turn this into an array?
Object.keys(mounted).map((key) => mounted[key])

Categories