Skip to content

Data Transformations

Ben edited this page Apr 15, 2020 · 3 revisions

The transform option is a powerful way to control the image data returned by Instagram before it is rendered by Instafeed.js. For example, you could use it to add extra fields to your images, adjust or format the values Instagram returns, etc.

The transform option accepts a function. That function will be applied to each item in the set returned by Instagram. The return value of the function will replace the item in the set.

Example:

Here's a function which formats the timestamp attribute to something friendlier:

var feed = new Instafeed({

  transform: function(item) { //Transform receives each item as its argument
    // Over-write the original timestamp
    item.timestamp = new Date(item.timestamp).toLocaleString('en-AU', {
        weekday: 'long', 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric'
      });

    // return the modified item
    return item;
  }

  // ...
});

Order of Operations

Transform is applied before filter, sort and limit. This allows you to filter or sort based on your transformed items if you need to.

Changes from v1

Instafeed v1 provided a filter option which was often used as an 'unofficial' transform. While filter still exists, it's better to use transform when you want to modify your item data.

Clone this wiki locally