Skip to content
Yasser Moradi edited this page Aug 30, 2015 · 3 revisions

Description:

A join can be achieved on any data as long as both data sources share a common column value. Although the concept of joining in-memory collections isn’t a common pattern today. This sample contains stock list & category list which have common property.

Note that there is an optional argument, named comparer at the end of arguments list, which accepts a function to make comparison customizable.

Samples:

let stock = [
  { name: 'Apple', category: 'Fruit', price: 0.30 },
  { name: 'Banana', category: 'Fruit', price: 0.35 },
  { name: 'Orange', category: 'Fruit', price: 0.29 },
  { name: 'Cabbage', category: 'Vegetable', price: 0.49 },
  { name: 'Carrot', category: 'Vegetable', price: 0.29 },
  { name: 'Lettuce', category: 'Vegetable', price: 0.30 },
  { name: 'Milk', category: 'Dairy', price: 1.12 }
];

let categories = [
  { name: 'Dairy', majorCategory: 'Chilled' },
  { name: 'Fruit', majorCategory: 'Fresh' },
  { name: 'Vegetable', majorCategory: 'Fresh' }
];

let joints = stock.asEnumerable()
  .join(
    categories.asEnumerable(),
    stockItem => stockItem.category,
    cat => cat.name,
    (stockItem, cat) =>
      "[" +
      "Name = " + stockItem.name + "," +
      "Price = " + stockItem.price + "," +
      "Category = " + cat.name + "," +
      "MajorCategory = " + cat.majorCategory +
      "]"
    ).toArray();

console.log("*** using join method for joining stockItems and categories");

for (let index = 0; index < joints.length; index++)
  console.log(joints[index]);
Clone this wiki locally