1

How can I convert this array:

['black', 'grey', 'dark grey'];

To this:

[ MyObject { name: 'black', description: 'colour is black' }, MyObject { ... } ]
2
  • What exactly is MyObject? how do you create them? Commented Nov 6, 2018 at 13:13
  • You can use map 😊 Commented Nov 6, 2018 at 13:15

3 Answers 3

4

A first straight forward solution: define an interface Colour, create a new array, here: colours, and iterate through your old array to push items into your new one:

interface Colour {
  name: string;
  description: string;
};

const colours: Colour[] = [];

['black', 'grey', 'dark grey'].forEach((colour: string) => {
  colours.push({
    name: colour,
    description: `colour is ${colour}`
  });
});

Using map instead:

const colours: Colour[] = ['black', 'grey', 'dark grey'].map(colour => {
  return {
    name: colour,
    description: `colour is ${colour}`
  };
});
Sign up to request clarification or add additional context in comments.

Comments

3

Simple version

['black', 'grey', 'dark grey'].map((color) => { return {name: color, description: `colour is ${color}`}});

Best/Better practice

interface Color {
  name: string;
  description: string;
}

type Colors = Color[];

const colors: string[] = ['black', 'grey', 'dark grey'];

colors.map((colour): Colors => ({ name: colour, description: `colour is ${colour}` }));

Comments

0

Another straight forward method using a global type declare (global declaration helps you use it anywhere in the entire project),

declare global { // you can still define type without declaring it globally
  type Dictionary<T> = { [key: string]: T };
  type DefaultResponse = { 
    success: boolean,
    message: string,
  }
}

interface MyObject { 
    name: string,
    description: string 
  }

and in your function,

  var items = ['black', 'grey', 'dark grey']
  var results:[Dictionary<MyObject> | []]  = []

  items.reduce((result, field, index) => { 
    var _obj:Dictionary<string| any> =  { name: field , description : `The colour is ${field}` }
    results.push(_obj)
    return result;
  }, {})

  console.log(results)

view it in playground action: PlayGround

PS: If you'll use this frequently for more array with different keys, here's the more generalized solution:

 const destructureArray = (expectedKeys:any) => { 
    var aligned:[Dictionary<MyObject> | []]  = []
    var description = "The colour is "
    expectedKeys.reduce((param:any, field:any, index:any) => { 
        var _obj:Dictionary<string| any> =  {  name: field, description: `${description}: ${field}` }
        param = Object.assign(param, _obj)
        aligned.push(_obj)
       return param;
    }, {})
    return aligned;
   }

and the function call,

var expected = ['black', 'grey', 'dark grey']
var parsed = destructureArray( expected)
console.log(parsed)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.