---
title: "CoffeeScript notes: write less do more"
author: "lingceng"
date: "2013-07-26T10:36:25Z"
tags: []
categories: ["Web前端"]
slug: "coffeescript-notes-write-less-do-more"
draft: false
lastmod: "2013-09-01T10:44:15Z"
id: 1913865
url: "https://lingceng.iteye.com/blog/1913865"
---

[The Little Book on CoffeeScript](http://arcturo.github.io/library/coffeescript/01_introduction.html)   [official site](http://coffeescript.org/)
 
>    CoffeeScript is a little language that compiles down to JavaScript. The syntax is inspired by Ruby and Python, and implements many features from those two languages.  

  I found some amazing features when using CoffeeScript on my project. Here I have some examples.
  **wonderful idioms of array and object**
   Array#forEach() Array#map() Array#filter() are latest features introduced in ECMAScript 5, CoffeeScript have compatible idioms to implement them.

```coffeecript

# forEach function
myFunction(item) for item in array

# map function
result = (item.name for item in array)

# filter and map
result = (item for item in array when item.name is "test")

```

   Demo: convert objects array to values array using coffeescript [online demo](http://runjs.cn/code/9aj3d9r5)

```coffeecript

# convert object to array in order
	objs = [ {
			sn: "dfjjk"
			project: "fly"
			department: "origin"
			feelings :"well"
		},{
			sn: "dfjjk"
			project: "fly"
			department: "origin"
			feelings :"well"
		}]
		
	order = ["project", "sn",  "feelings", "department"]

	result =  
		for obj in objs
			for name in order 
				obj[name] 
				
	# or in simple way	
	#result = ((obj[name] for name in order ) for obj in objs)
				
	console.log result
	for r in result
				$("body").append r + "<br/>"

```