I have a multidimensional array that looks like this:
[
Object {href=".png", title="Wheel"},
Object {href=".png", title="Top"},
Object {href=".png", title="Side"},
Object {href=".png", title="Saddle"},
Object {href=".png", title="Front"}
]
I'd like to loop over this object and retrieve two ma seperated strings, one for all hrefs and one for all titles. So what I'm after is:
hrefs = "'.png', '.png', '.png', '.png', '.png'";
titles = "'Wheel', 'Top', 'Side', 'Saddle', 'Front'";
Although it seems pretty easy I lack the knowledge and cant seem to find the specific answer.
I have a multidimensional array that looks like this:
[
Object {href="http://www.somepath./car4.png", title="Wheel"},
Object {href="http://www.somepath./car.png", title="Top"},
Object {href="http://www.somepath./car1.png", title="Side"},
Object {href="http://www.somepath./car5.png", title="Saddle"},
Object {href="http://www.somepath./car6.png", title="Front"}
]
I'd like to loop over this object and retrieve two ma seperated strings, one for all hrefs and one for all titles. So what I'm after is:
hrefs = "'http://www.somepath./car4.png', 'http://www.somepath./car.png', 'http://www.somepath./car1.png', 'http://www.somepath./car5.png', 'http://www.somepath./car6.png'";
titles = "'Wheel', 'Top', 'Side', 'Saddle', 'Front'";
Although it seems pretty easy I lack the knowledge and cant seem to find the specific answer.
Share Improve this question edited Oct 22, 2014 at 12:21 danmullen 2,5003 gold badges22 silver badges28 bronze badges asked Oct 22, 2014 at 12:08 SolideSolide 1892 silver badges7 bronze badges 04 Answers
Reset to default 5A simple function that uses map
to get the values into an array, and then join them up.
const arr=[{href:"http://www.somepath./car4.png",title:"Wheel"},{href:"http://www.somepath./car.png",title:"Top"},{href:"http://www.somepath./car1.png",title:"Side"},{href:"http://www.somepath./car5.png",title:"Saddle"},{href:"http://www.somepath./car6.png",title:"Front"}];
function createString(arr, key) {
return arr
.map(obj => `'${obj[key]}'`)
.join(', ');
}
console.log(JSON.stringify(createString(arr, 'href')));
console.log(JSON.stringify(createString(arr, 'title')));
Additional documentation
- Template/string literals
You could do this:
var hrefs = [],
titles = [];
data.forEach(function(obj) {
hrefs.push("'" + obj.href + "'");
titles.push("'" + obj.title + "'");
});
hrefs = hrefs.join(', ');
titles = titles.join(', ');
var data = [
{href:"http://www.somepath./car4.png", title:"Wheel"},
{href:"http://www.somepath./car.png", title:"Top"},
{href:"http://www.somepath./car1.png", title:"Side"},
{href:"http://www.somepath./car5.png", title:"Saddle"},
{href:"http://www.somepath./car6.png", title:"Front"}
],
href = [],
title = [],
dataL = data.length;
for ( i = 0; i < dataL; i++ ){
href.push ( data[i]['href'] );
title.push ( data[i]['title'] );
}
href = href.join(",");
title = title.join(",");
The canonical function to make an array based on the contents of another array is map
. You can then bine that with join
to make the required string:
var hrefs = data.map(function(d) { return "'" + d.href + "'" }).join(', ');
var titles = data.map(function(d) { return "'" d.title + "'" }).join(', ');
Note that this would fail to produce valid CSV if there were any single quote signs inside the fields. A solution would be to supply a more rigorous quoting function. Following the usual convention that a quote inside a string should be doubled-up:
function enquote(v) {
return "'" + v.replace(/'/g, "''") + "'";
}
with usage:
var titles = data.map(function(d) { return enquote(d.title) }).join(', ');