This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
var v = [{"filename":"1.mp4","resname":"1280x720","videolength":"00:00:26.07"},{"filename":"2.mp4","resname":"854x480","videolength":"00:00:26.07"},{"filename":"3.mp4","resname":"640x360","videolength":"00:00:26.07"}];
I am using $.parseJSON(v); but have error , any suggestion to get the value of resname ?
Try:
var v = [{"filename":"1.mp4","resname":"1280x720","videolength":"00:00:26.07"},{"filename":"2.mp4","resname":"854x480","videolength":"00:00:26.07"},{"filename":"3.mp4","resname":"640x360","videolength":"00:00:26.07"}];
$.each(v,function (i, val) {
console.log(val.resname)
});
There is no need to parse v you can access it in different ways
one is by iteration or forloop
v.forEach(function (item, i) {
alert(item.resname)
});
and you can also directly access the elements by using
alert(v[0].filename);
alert(v[1].filename);
Related
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
let arr = [{
question:{
q1 : 123,
q2 : 44
}
}];
I've tried using forEach loop to do it, but it returns nothing.
If it's an array then you can do either map or forEach.
arr.forEach()
However you have to pay attention to the internal structure of the item as well, ex. in your case
arr.forEach(item => {
console.log(item.question.q1)
})
And if you want the transformation, then
const transformedArr = arr.map(item => item.question.q1)
This question already has answers here:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle
This question already has answers here:
How to loop through a plain JavaScript object with the objects as members
(28 answers)
Closed 9 years ago.
{
"USA":[{"country":"Albuquerque (ABQ)"},
{"country":"Allentown (ABE)"}
],
"Canada":{"country":"Calgary (YYC)"},
"Hawaii":{"country":"Honolulu International Apt (HNL)"}
}
This is my object I need to loop it through so i can get all the values like this
USA
Albuquerque (ABQ)
Allentown (ABE)
Canade
Calgary (YYC)
For the solution you can check out this demo.
DEMO
You can loop Object using for(k in obj)
for(k in obj){
var value = obj[k];
}
I have created a jsfiddle for you at http://jsfiddle.net/5eM4q/
The way you access the data using
data[obj].country
is incorrect
This question already has answers here:
read name of unknown properties
(2 answers)
Closed 8 years ago.
I'm working with wikipedia API and have a problem with the results I'm getting.
{"query":{
"pages":{
"48636":{
"pageid":48636,
I don't know what the ID is going to be when I make the call, how do I access the 48636?
if gon.activateWiki
$(gon.keywords).each (index, element) ->
console.log element.name
$.getJSON( 'http://sv.wikipedia.org/w/api.php?action=query&format=json&prop=revisions%7Clinks%7Cimages%7Ccategories&rvprop=content&titles=' + element.name + '&callback=?' , (data) ->
console.log data.query.pages
)
You want the trusty old Object.keys
firstKey = Object.keys(data.query.pages).shift()
lastKey = Object.keys(data.query.pages).pop()
nthKey = Object.keys(data.query.pages)[n-1]
firstPage = data.query.pages[firstKey]
lastPage = data.query.pages[lastKey]
nthPage = data.query.pages[n-1]
You can access it like an array:
console.log(data.query.pages["48636"]);
Update:
To get values from unknown property's of object you need to use for..in statement. Or you can use slice method and transform your object to array:
var pages = Array.prototype.slice.call(data.query.pages);