Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ‚̧ ūüíĽ

If you think sharing knowledge and helping other is valuable.

join our community - Click here

TypeScript - array reduce method example

0 contributions
11 points

In TypeScript it is possible to convert array using reduce method in following ways.

1. Array conversion example

let array = [
    {
        id : 1,
        group : 'Juices',
        items : [
            { name : 'Apple',   price : 1.4 },
            { name : 'Orange',  price : 1.7 }
        ]
    },
    {
        id : 2,
        group : 'Baking',
        items : [
            { name : 'Bread',   price : 1.3 },
            { name : 'Roll',    price : 0.5 },
            { name : 'Loaf',    price : 1.1 }
        ]
    },
    {
        id : 3,
        group : 'Meat',
        items : [
            { name : 'Salmon',  price : 15.0 },
            { name : 'Turey',   price : 6.5  },
            { name : 'Chicken', price : 7.8  }
        ]
    },
];

let reduce = (array : Array<any>, entry : any, index : number) : Array<any> => {
    [...array, ...entry.items];
};

let products = array.reduce(reduce, new Array<any>());

console.log(products);

Where:

  • array.reduce(reduce, new Array<any>())¬†method has been used to convert array of groups¬†to array of items with initial empty array (second reduce method argument),
  • let reduce = (array : Array<any>, entry : any) : Array<any> ...¬†method contains conversion and merging¬†logic for each array¬†element
    where:
    • array : Array<any>¬†variable indicates¬†array returned during previous¬†iteration or¬†new Array<any>()¬†object if it is first iteration,
    • entry : any variable contains currently iterated element,
    and returns array for next iteration or reduce operation result if all elements has been iterated.

Output (with NodeJS):

[
  { name: 'Apple',   price: 1.4 },
  { name: 'Orange',  price: 1.7 },
  { name: 'Bread',   price: 1.3 },
  { name: 'Roll',    price: 0.5 },
  { name: 'Loaf',    price: 1.1 },
  { name: 'Salmon',  price: 15  },
  { name: 'Turey',   price: 6.5 },
  { name: 'Chicken', price: 7.8 } 
]

2. Item finding example

let array = [
    {
        id : 1,
        group : 'Juices',
        items : [
            { name : 'Apple',   price : 1.4 },
            { name : 'Orange',  price : 1.7 }
        ]
    },
    {
        id : 2,
        group : 'Baking',
        items : [
            { name : 'Bread',   price : 1.3 },
            { name : 'Roll',    price : 0.5 },
            { name : 'Loaf',    price : 1.1 }
        ]
    },
    {
        id : 3,
        group : 'Meat',
        items : [
            { name : 'Salmon',  price : 15.0 },
            { name : 'Turey',   price : 6.5  },
            { name : 'Chicken', price : 7.8  }
        ]
    },
];

let reduce = (result : any, entry : any, index : number) : any => {
    if(entry.items.length < result.items.length)
        return entry;

    return result;
};

let smallestGroup = array.reduce(reduce);

console.log(smallestGroup);

Output (with NodeJS):

{
  id: 1,
  group: 'Juices',
  items: [
    { name: 'Apple',  price: 1.4 },
    { name: 'Orange', price: 1.7 }
  ]
}

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more