SQLDBA SQLDBA - 6 months ago 55
Javascript Question

Aurelia - repeat.for json

I am new to Aurelia.

I'm trying to iterate (repeat.for) through some json that looks like below. I am able to do so with a manual concat but the number of artists is variable. Below for example works.

return this.http.fetch(baseUrl + "/artists/" + token + "&search=" + searchCreator)
.then(response => response.json())
.then(response => {
//for (var i = 0; i < response.resultsCount; i++){
// response += response.artists[i].objects;
//}
return response.artists[0].objects.concat(response.artists[1].objects);
});


How can I return all objects for all artists? I've tried doing this with a for loop but I can't get it to work. Should I return response.artists and then work with that in my view model? This is what the view looks like:

<tr repeat.for="item of items">
<td>${item.title}</td>
<td>${item.displayName}</td>
<td>${item.objectNumber}</td>
</tr>


json example:

{

"source": "My Museum",
"language": "EN",
"resultsCount": ​58,
"artists":

[{

"artistID": ​47171,
"alphaSort": "Smith Eloise Vega",
"displayName": "Eloise Vega Smith",
"beginDate": "0",
"endDate": "0",
"displayDate": "",
"sex": "Female",
"nationality": "",
"objectCount": ​1,
"objects":

[{
"objectNumber": "209.2015",
"objectID": ​188963,
"title": "Album cover for Urszula Dudziak, Newborn Light",
"displayName": "Eloise Vega Smith",
"alphaSort": "Smith Eloise Vega",
"artistID": ​47171,
"displayDate": "",
"dated": "1974",
"dateBegin": ​1974,
"dateEnd": ​1974,
"medium": "Lithograph",
"dimensions": "12 1/2 x 12 1/4\" (31.8 x 31.1 cm)",
"department": "Architecture & Design",
"classification": "A&D Graphic Design",
"onView": ​0,
"provenance": "",
"description": "",
"objectStatusID": ​1,
"creditLine": "Committee on Architecture and Design Funds",
"imageID": "502407",
"thumbnail": "http:///TMSImages/Size1/Images/TR15211_10_RICR.jpg",
"fullImage": "http:///TMSImages/Size3/Images/TR15211_10_RICR.jpg",
"lastModifiedDate": "2015-09-17T01:00:08"
}
]

},
{

"artistID": ​5479,
"alphaSort": "Smith Charles",
"displayName": "Charles Smith",
"beginDate": "1893",
"endDate": "1987",
"displayDate": "American, 1893–1987",
"sex": "Male",
"nationality": "American",
"objectCount": ​6,
"objects":

[{

"objectNumber": "369.1941",
"objectID": ​67911,
"title": "Red Circle",
"displayName": "Charles Smith",
"alphaSort": "Smith Charles",
"artistID": ​5479,
"displayDate": "American, 1893–1987",
"dated": "1940",
"dateBegin": ​1940,
"dateEnd": ​1940,
"medium": "Monoprint",
"dimensions": "Sheet 21 1/8 x 14 3/4\" (53.8 x 37.5 cm) Comp. 20 x 14 3/4\" (50.8 x 37.5 cm)",
"department": "Prints & Illustrated Books",
"classification": "Print",
"onView": ​0,
"provenance": "",
"description": "Color monoprint, printed from movable forms",
"objectStatusID": ​1,
"creditLine": "Purchase",
"imageID": "260293",
"thumbnail": "http:///TMSImages/Size1/Images/369_1941_RICR.jpg",
"fullImage": "http:///TMSImages/Size3/Images/369_1941_RICR.jpg",
"lastModifiedDate": "2015-03-31T01:06:18"

},
{

"objectNumber": "214.1942",
"objectID": ​64517,
"title": "Abstraction",
"displayName": "Charles Smith",
"alphaSort": "Smith Charles",
"artistID": ​5479,
"displayDate": "American, 1893–1987",
"dated": "c. 1942",
"dateBegin": ​1942,
"dateEnd": ​1942,
"medium": "Woodcut",
"dimensions": "composition (irreg.): 11 1/4 x 3 3/4\" (28.5 x 9.6 cm); sheet: 14 9/16 x 7 1/16\" (37 x 17.9 cm)",
"department": "Prints & Illustrated Books",
"classification": "Print",
"onView": ​0,
"provenance": "",
"description": "Woodcut, printed in black, dark green yellow, deep blue and strong red brown",
"objectStatusID": ​1,
"creditLine": "Given anonymously",
"imageID": "195213",
"thumbnail": "http:///TMSImages/Size1/Images/214_1942_RICR.jpg",
"fullImage": "http:///TMSImages/Size3/Images/214_1942_RICR.jpg",
"lastModifiedDate": "2014-09-19T01:05:22"

},
etc.....


I appreciate the help!

Answer

If you are familiar with LINQ, you could use a library like jslinq and do the following:

return jslinq(response.artists)
    .selectMany(function(artist){
        return artist.objects
    }).toList();

of course I would only include jslinq if I planned to use it more than once in the project. If you don't want the overhead, your solution with concat should work just fine:

var result = [];
for(var i = 0; i < response.artists.length; i++) {
    result = result.concat(response.artists[i].objects);
}
return result;
Comments