Reputation: 323
When I console.log the data that I am getting, I'm getting an object that looks like this (I trimmed some stuff to make it look more readable and the ... is basically a repeat of the info with different values):
[ Submission {
title: 'Untitled',
content:
{ url: 'http://orig00.deviantart.net/8791/f/2016/079/7/b/untitled_by_wecode-d9vvg2w.png',
height: '400',
width: '400',
medium: 'image' },
text: null },
Submission {
...
},
Submission {
...
} ]
I want to grab the url from content in each submission. At the end, I should get an array with multiple urls. I'm working in nodejs if that is of any help.
Upvotes: 0
Views: 59
Reputation: 1449
var yourObject = [{
Submission: {
title: 'Untitled',
content: {
url: 'http://orig00.deviantart.net/8791/f/2016/079/7/b/untitled_by_wecode-d9vvg2w.png',
height: '400',
width: '400',
medium: 'image'
},
text: null
}
}];
// your new array of url's
var urls = [];
for (i = 0; i < yourObject.length; i++) {
urls.push(yourObject[i].Submission.content.url);
}
Upvotes: 0
Reputation:
Use .map, which creates a new array by applying the function to each element in the old array.
var arrayOfSubmissions = ...
var arrayOfURLs = arrayOfSubmissions.map(function(submission){
return submission.content.url
})
Upvotes: 3
Reputation: 8523
.map
is your friend. It iterates over every item of an array and will add whatever each run of a function returns to a new array.
var urls = yourArray.map(function (item) {
return item.content.url;
});
Upvotes: 2
Reputation: 2961
So you have an array of Objects here - let's assume it's stored in a variable called myArray
. To get a new list of just the URLs, you can do:
var listOfUrls = [];
myArray.forEach(function(obj) {
listOfUrls.push(obj.content.url);
}
This will give you the array you want. This is a simple way to do it, but you can also do it in other ways such as map
Upvotes: 1