Skip to content

How to create a cleaned up contact object of an array of contact data

agershun edited this page Dec 28, 2014 · 1 revision

How to create a cleaned up contact object of an array of contact data?

Source: StackOverflow.com

Question

There is an array with objects like this:

	[
		{
			id: '1',
			name: 'Joe',
			description: 'Student',
			locations: [ {
				type: 'home',
				value: '123'
			} ]
		},
		{
			id: '1',
			name: 'Joe',
			description: 'Farmer',
			locations: [ {
				type: 'home',
				value: '456'
			} ]
		},
		{
			id: '1',
			name: 'Joe',
			description: '',
			locations: [ {
				type: 'home',
				value: '123'
			} ]
		}
	]

How to create the following? :

	{
		id: '1',
		name: 'Joe',
		description: 'Farmer',
		locations: [ {
			type: 'home',
			value: '123'
		}, {
			type: 'home',
			value: '456'
		} ]
	}

Answer

    var res = alasql('SELECT id, FIRST(name) AS name, FIRST(description) AS description, \
                            ARRAY(locations->0) AS locations FROM ?',[data]);

See the example at jsFiddle

Clone this wiki locally