Given the below response, how would I get things like title?
jsonFlickrFeed({
"title": "Thing",
"link": "http://www.flickr.com/photos/tags/",
"description": "",
"items": [
{
"title": "Title",
"link": "http://www.flickr.com/photos/123",
"media": {"m":"http://farm6.staticflickr.com/123.jpg"},
},
{
"title": "Title2",
"link": "http://www.flickr.com/photos/1234",
"media": {"m":"http://farm6.staticflickr.com/1234.jpg"},
},
})
I want to do something like:
for (var i=0; i<xml.responseText.length;i++) {
var x = (xml.responseText.items.title[i]);
document.write(x)
}
What am I doing wrong here?
assuming xml.responseText is not null and is the defined result of the function in your question...
reference the responseText.items length, not responseText. Then move your index from title to items:
for (var i=0; i < xml.responseText.items.length; i++) {
var x = xml.responseText.items[i].title;
document.write(x)
}
Related
I am trying to get a list together from an array but some of the fields are null and it seems to be breaking there and returning:
obj.names is null
Here is the code:
My Code
var data = {
"people": [{
"id": "32",
"description": "some description",
"archived": "",
"new": 0,
"names": [{
"name": "name 1",
"translations": null
}, {
"name": "name 2",
"translations": null
}],
}, {
"id": "56",
"description": "some description",
"archived": "",
"new": 0,
"names": [{
"name": "name 3",
"translations": null
}, {
"name": "name 4",
"translations": null
}],
}, {
"id": "99",
"description": "some description",
"archived": "",
"new": 0,
"names": null,
},
]
};
var mainData = [data];
var namesList = [];
for (var i = 0; i < mainData[0].people.length; i++) {
var obj = mainData[0].people[i];
var nme = obj.names.name;
namesList.push(nme);
}
console.log(namesList); //This should have the list of names
<div id="container"></div>
JSFiddle Link
How can I fix this?
names is an array, you need to get the name property from the objects inside the names array.
Replace these lines
var nme = obj.names.name;
namesList.push(nme);
by
if( obj.names )
{
var nme = obj.names.map( function(item){
return item.name;
});
namesList = namesList.concat(nme);
}
map method is used to iterate the array and return the name property of each array item.
I prefer using array methods like map/reduce/forEach.
You end up with something like this:
// we want to loop over the people array, but it's not a 1-to-1 relation, since we want all name in the names array, so we use reduce into new array.
var namesList = data.people.reduce(function( ary, peopleRecord ) {
// We want to concat the names from the names array to our result
return ary.concat(
// but if the names array is null, we have nothing to concat, so check if the names array exists
peopleRecord.names && peopleRecord.names.map(function( nameRecord ) {
return nameRecord.name;
// Or else if the names array doesn't exist, just concat an empty array to the final result.
}) || []
);
}, []);
I'm working on Bootstrap calendar and I've tried to make my array looks like this:
{
"success": 1,
"result": [
{
"id": "293",
"title": "This is warning class event with very long title to check how it fits to evet in day view",
"url": "http://www.example.com/",
"class": "event-warning",
"start": "1362938400000",
"end": "1363197686300"
},
{
"id": "256",
"title": "Event that ends on timeline",
"url": "http://www.example.com/",
"class": "event-warning",
"start": "1363155300000",
"end": "1363227600000"
},
{
"id": "276",
"title": "Short day event",
"url": "http://www.example.com/",
"class": "event-success",
"start": "1363245600000",
"end": "1363252200000"
},
{
"id": "294",
"title": "This is information class ",
"url": "http://www.example.com/",
"class": "event-info",
"start": "1363111200000",
"end": "1363284086400"
}
] }
But I've tried and got something like this:
[
{
"success":1
},
{
"result":[
{
"id":"1",
"title":"dev",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"2",
"title":"Holiday",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"3",
"title":"Coding...",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"4",
"title":"data",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
}
]
}
]
and my code to create those mess are
var result = [];
for(i=0; i<rows.length; i++) {
var result_single = {id:rows[i].uid, title:rows[i].name, url:"www.example.com", class:"event-success", start:rows[i].startdate, end:rows[i].enddate};
result.push(result_single);
}
var output = new Array();
output.push(data);
result = {result:result};
output.push(result);
console.log(JSON.stringify(output));
To be honest, I don't quite understand how array in js works. can somebody point out how did i go wrong?
Your expected result is an object which contains 2 properties, but you get an array which contains two objects. I think you want the first result which is object with two properties.
var result = [];
var rows = [1,2,3,4,5];
for(i=0; i<rows.length; i++) {
var result_single = {id:rows[i], title:rows[i], url:"www.example.com", class:"event-success", start:rows[i].startdate, end:rows[i].enddate};
result.push(result_single);
}
var output = {
success: 1,
result: result
}
console.log(JSON.stringify(output));
I have following array:
var array = [
{
"milestoneTemplate": {
"id": "1",
"name": "TEST1"
},
"id": "1",
"date": "1416680824",
"type": "ETA",
"note": "Note",
"color": "66FF33"
},
{
"milestoneTemplate": {
"id": "2",
"name": "Test 2"
},
"id": "2",
"date": "1416680824",
"type": "ATA",
"note": "Note 22",
"color": "66FF00"
}
];
And now i would like to check in forEach loop that object (which is passed in param of the function) is existing in array by his ID.
In case that not = do push into existing array.
arrayOfResults.forEach(function(entry) {
if(entry != existingInArrayByHisId) {
array.push(entry);
}
});
Thanks for any advice
You could create a helper function thats checks if an array contains an item with a matching property value, something like this:
function checkForMatch(array, propertyToMatch, valueToMatch){
for(var i = 0; i < array.length; i++){
if(array[i][propertyToMatch] == valueToMatch)
return true;
}
return false;
}
which you can then use like so:
arrayOfResults.forEach(function (entry) {
if (!checkForMatch(array, "id", entry.id)) {
array.push(entry);
}
});
Here is my JSON array code
var App = [
{
"id": "123",
"caption": "Test",
"description": "Test Desc"
},
{
"id": "345",
"caption": "adsasdasd",
"description": ""
},
{
"id": "456",
"caption": "adsasdasd",
"description": ""
},
{
"id": "578",
"caption": "adsasdasd",
"description": ""
}
]
i tried with the following code
var obj = $.parseJSON(App);
alert(JSON.stringify(obj,4,null));
var AppLen = obj[i].length;
alert(AppLen);
but i didn't get any solution. Let me know if i missed any thing to get the JSON object array length.
your data is already json format:do like this
var App = [
{
"id": "123",
"caption": "Test",
"description": "Test Desc"
},
{
"id": "345",
"caption": "adsasdasd",
"description": ""
},
{
"id": "456",
"caption": "adsasdasd",
"description": ""
},
{
"id": "578",
"caption": "adsasdasd",
"description": ""
}
];
console.log(App);
console.log(App[0].length);// you can not get length from this because it is not array it's an object now.
var AppLen = App.length;
alert(AppLen);
obj.size() or obj.length
If that dont run try this: Length of a JavaScript object
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
var size = Object.size(myArray);
I need assistance in accessing a nested array located my JSON Data Set. Here is the first entry of my top-level JSON array:
{
"pingFeed": [{
"header": "Get Drinks?",
"picture": "images/joe.jpg",
"location": "Tartine's, SF",
"time": "Tomorrow Night",
"name": "Joe Shmoe",
"pid":
"123441121",
"description": "Let's drop some bills, yal!",
"comments": [{
"author": "Joe S.",
"text": "I'm Thirsty"
},
{
"author": "Adder K.",
"text":
"Uber Narfle"
},
{
"author": "Sargon G.",
"text": "taeber"
},
{
"author": "Randy T.",
"text": "Powdered Sugar"
},
{
"author": "Salvatore D.",
"text":
"Chocolate with Sprinkles"
},
{
"author": "Jeff T.",
"type": "Chocolate"
},
{
"author": "Chris M.",
"text": "Maple"
}],
"joined": false,
"participants": [
"Salvatore G.", "Adder K.", "Boutros G."],
"lat": 37.25,
"long": 122,
"private": true
}]
}
I would like to know how I can access the comments and participants data using the following notation:
for (var k = 0; k < pingFeed.length ; k++) {
console.log(pingFeed[k].comments);
console.log(pingFeed[k].participants);
}
Currently this form of dot notation is working for the other entries in the JSON array...
I am looking to return all of these data as Strings.
I'm not sure quite what you're looking to do, but perhaps this will point you in the right direction:
for (var k = 0; k < pingFeed.length; k++) {
for (var i = 0; i < pingFeed[k].comments.length; i++) {
var oComments = pingFeed[k].comments[i];
console.log( oComments.author + ": " + oComments.text );
}
console.log(pingFeed[k].participants.join(", "));
}
Well, comments and participants are arrays, so you can access them like normal arrays, e.g.:
for (var k = 0; k < pingFeed.length ; k++) {
var comments = pingFeed[k].comments;
for(var i = 0, length = comments.length; i < length; ++i) {
console.log(comments[i]);
}
}
There's nothing wrong with your code: pingFeed[k].comments will return array and pingFeed[k].comments[0] will return first comment from that array.
Try here
http://jsfiddle.net/U8udd/