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
  • stackoverflow.com/q/26575018/5026957 – HarisH Sharma Commented Mar 7, 2019 at 8:39
  • 1 It depends on how is defined the class State, and btw it's not related to Angular. Angular is just a framework, that is just a typescript functionality – Christian Vincenzo Traina Commented Mar 7, 2019 at 8:41
  • @CristianTraìna actually TypeScript is just a dialect of JavaScript, and is not existing in runtime. This is javascript functionality :) – smnbbrv Commented Mar 7, 2019 at 8:42
  • 1 As mentioned above is related to JavaScript, you can use map function to extract values like testArray.map((state) => { return state.name}) – Nikhil Walvekar Commented Mar 7, 2019 at 8:45
  • @NikhilWalvekar or simply testArray.map( state => state.name ) – Jeremy Thille Commented Mar 7, 2019 at 8:46
 |  Show 1 more comment

3 Answers 3

Reset to default 15

The 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