2

I have a multidimensional array that looks like this:

[
    Object {href="http://www.somepath.com/car4.png", title="Wheel"},
    Object {href="http://www.somepath.com/car.png", title="Top"},
    Object {href="http://www.somepath.com/car1.png", title="Side"},
    Object {href="http://www.somepath.com/car5.png", title="Saddle"},
    Object {href="http://www.somepath.com/car6.png", title="Front"}
]

I'd like to loop over this object and retrieve two comma seperated strings, one for all hrefs and one for all titles. So what I'm after is:

hrefs = "'http://www.somepath.com/car4.png', 'http://www.somepath.com/car.png', 'http://www.somepath.com/car1.png', 'http://www.somepath.com/car5.png', 'http://www.somepath.com/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.

0

4 Answers 4

5

A simple function that uses map to get the values into an array, and then join them up.

const arr=[{href:"http://www.somepath.com/car4.png",title:"Wheel"},{href:"http://www.somepath.com/car.png",title:"Top"},{href:"http://www.somepath.com/car1.png",title:"Side"},{href:"http://www.somepath.com/car5.png",title:"Saddle"},{href:"http://www.somepath.com/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

Sign up to request clarification or add additional context in comments.

3 Comments

@Andy agreed - this is clearly just a "formatted" representation of the object, not actual JS.
@Andy ok: I was confused by the csv and multi-dimensional array tags, which are in this case misleading
I have choosen this as the correct answer as it gives me exactly what I wanted in a clean way.
0

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(', ');

6 Comments

Just a quick note - forEach is supported in IE9 and above, in case anybody needs to support previous versions.
@Vld—though a for loop is simple, often faster and can be less to type.
It really is about time we stopped having to mention polyfills. ES5 has been out 5 years now - enough is enough!
When people don't have to support IE8 and lower, perhaps.
|
0
var data = [
    {href:"http://www.somepath.com/car4.png", title:"Wheel"},
    {href:"http://www.somepath.com/car.png", title:"Top"},
    {href:"http://www.somepath.com/car1.png", title:"Side"},
    {href:"http://www.somepath.com/car5.png", title:"Saddle"},
    {href:"http://www.somepath.com/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(",");

Comments

0

The canonical function to make an array based on the contents of another array is map. You can then combine 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(', ');

3 Comments

This answer looks fine. Why downvote? care to share the reason please.
I assume it's because the OP doesn't want an array. The OP wants a string of quoted items.
@Andy good point - didn't notice that (was distracted by a colleague). Updated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.