admin管理员组文章数量:1292618
I have the following array (testArray) and i wish to extract all the states to string ('Arizona','Alaska','Florida','Hawaii','Gujarat','Goa','Punjab'...). Are there any simpler ways to do that?
let testArray: State[];
testArray = this.getStates();
getStates() {
return [
new State(1, 1, 'Arizona'),
new State(2, 1, 'Alaska'),
new State(3, 1, 'Florida'),
new State(4, 1, 'Hawaii'),
new State(5, 2, 'Gujarat'),
new State(6, 2, 'Goa'),
new State(7, 2, 'Punjab'),
new State(8, 3, 'Queensland'),
new State(9, 3, 'South Australia'),
new State(10, 3, 'Tasmania'),
new State(11, 4, 'Penang')
];
}
I have the following array (testArray) and i wish to extract all the states to string ('Arizona','Alaska','Florida','Hawaii','Gujarat','Goa','Punjab'...). Are there any simpler ways to do that?
let testArray: State[];
testArray = this.getStates();
getStates() {
return [
new State(1, 1, 'Arizona'),
new State(2, 1, 'Alaska'),
new State(3, 1, 'Florida'),
new State(4, 1, 'Hawaii'),
new State(5, 2, 'Gujarat'),
new State(6, 2, 'Goa'),
new State(7, 2, 'Punjab'),
new State(8, 3, 'Queensland'),
new State(9, 3, 'South Australia'),
new State(10, 3, 'Tasmania'),
new State(11, 4, 'Penang')
];
}
Share
Improve this question
edited Mar 7, 2019 at 9:17
Sourabh Somani
2,1381 gold badge14 silver badges27 bronze badges
asked Mar 7, 2019 at 8:36
scholarwithfirescholarwithfire
3652 gold badges9 silver badges24 bronze badges
6
|
Show 1 more comment
3 Answers
Reset to default 15The simplest way is as follows
getStates().map(x=>x.StateName).join(",")
Example is given below
function State(id,val,StateName) {
this.id = id;
this.val = val;
this.StateName = StateName;
}
function getStates() {
return [
new State(1, 1, 'Arizona'),
new State(2, 1, 'Alaska'),
new State(3, 1, 'Florida'),
new State(4, 1, 'Hawaii'),
new State(5, 2, 'Gujarat'),
new State(6, 2, 'Goa'),
new State(7, 2, 'Punjab'),
new State(8, 3, 'Queensland'),
new State(9, 3, 'South Australia'),
new State(10, 3, 'Tasmania'),
new State(11, 4, 'Penang')
];
}
//Simplest Way is as follows
console.log(getStates().map(x=>x.StateName).join(","))
Well the most simplest way to convert an array
to comma-separated string
is
var commaSeperatedString = arrayName.toString();
Another solution using reduce.
getStates().reduce((current, value, index) => {
if(index > 0)
current += ',';
return current + value.StateName;
}, '');
As a complete snippet:
function State(id,val,StateName) {
this.id = id;
this.val = val;
this.StateName = StateName;
}
function getStates() {
return [
new State(1, 1, 'Arizona'),
new State(2, 1, 'Alaska'),
new State(3, 1, 'Florida'),
new State(4, 1, 'Hawaii'),
new State(5, 2, 'Gujarat'),
new State(6, 2, 'Goa'),
new State(7, 2, 'Punjab'),
new State(8, 3, 'Queensland'),
new State(9, 3, 'South Australia'),
new State(10, 3, 'Tasmania'),
new State(11, 4, 'Penang')
];
}
var merged = getStates().reduce((current, value, index) => {
if(index > 0)
current += ',';
return current + value.StateName;
}, '');
console.log(merged);
本文标签: javascriptAngular Convert Array into stringStack Overflow
版权声明:本文标题:javascript - Angular Convert Array into string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738485491a2089390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
testArray.map((state) => { return state.name})
– Nikhil Walvekar Commented Mar 7, 2019 at 8:45testArray.map( state => state.name )
– Jeremy Thille Commented Mar 7, 2019 at 8:46