Is there a way to convert my array into a string using only one of the properties of each object?
Given:
[{f1:'v1', f2:'v21'}, {f1:'v2', f2:'v22'}, {f1:'v3', f2:'v23'}]
Desired
'v21,v22,v23'
Is there a way to convert my array into a string using only one of the properties of each object?
Given:
[{f1:'v1', f2:'v21'}, {f1:'v2', f2:'v22'}, {f1:'v3', f2:'v23'}]
Desired
'v21,v22,v23'
let input = [{
f1: 'v1',
f2: 'v21'
}, {
f1: 'v2',
f2: 'v22'
}, {
f1: 'v3',
f2: 'v23'
}];
let output = input.map((item) => item.f2).join(',');
console.log(output);
f1, f2 etc in quotesreduce ?map and join is reasonably clean.=>) or let's, so if this is running on the client and you need to support IE, you may need to re-write this with a normal function and var's.Using reduce()
var arr = [{"f1":"v1","f2":"v21"},{"f1":"v2","f2":"v22"},{"f1":"v3","f2":"v23"}];
var res = arr.reduce((a, e)=>{a.push(e.f2); return a },[]).toString()
console.log(res)