3

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')
 ];
}
Sourabh Somani
  • 2,002
  • 1
  • 9
  • 23
scholarwithfire
  • 259
  • 1
  • 6
  • 17
  • https://stackoverflow.com/q/26575018/5026957 – harish sharma Mar 07 '19 at 08: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 – Cristian Traìna Mar 07 '19 at 08:41
  • @CristianTraìna actually TypeScript is just a dialect of JavaScript, and is not existing in runtime. This is javascript functionality :) – smnbbrv Mar 07 '19 at 08: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 Mar 07 '19 at 08:45
  • @NikhilWalvekar or simply `testArray.map( state => state.name )` – Jeremy Thille Mar 07 '19 at 08:46
  • Possible duplicate of [Perform .join on value in array of objects](https://stackoverflow.com/questions/16607557/perform-join-on-value-in-array-of-objects) and [Javascript turn property of objects in array into a string](https://stackoverflow.com/questions/37037799) and [How to convert object into string](https://stackoverflow.com/questions/27815394) – adiga Mar 07 '19 at 09:06

3 Answers3

9

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(","))
Sourabh Somani
  • 2,002
  • 1
  • 9
  • 23
1

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);
Mathyn
  • 1,943
  • 1
  • 17
  • 26
1

Well the most simplest way to convert an array to comma-separated string is

var commaSeperatedString = arrayName.toString(); 
Wasim
  • 458
  • 3
  • 19