Created
May 24, 2012 11:02
-
-
Save stugoo/2780833 to your computer and use it in GitHub Desktop.
array builder for nesting arrays, checking categories and flange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var items = []; | |
// We're emulating indexOf, but for objects in the array. | |
// The function returns an integer (>=0), to the object that matched. | |
function in_property_array(property, value, arr) { | |
var i, max = arr.length, current; | |
for (i = 0; i < max; i++) { | |
if (arr[i][property] === value) { | |
return i; | |
} | |
} | |
return false; | |
} | |
$('a.reading-list').click(function(){ | |
var el = $(this); | |
var category = el.data('category'); | |
var id = el.data('id'); | |
var id_index = in_property_array('id', id, items); | |
var cat_index = in_property_array('category', category, items); | |
var details = { | |
title: el.text(), | |
url: el.attr('href'), | |
id: id | |
}; | |
if( id_index !== false ) { | |
console.log('id exists > do nothing'); | |
// return false; | |
} else if( cat_index !== false ) { | |
console.log('category exists'); | |
var cat_details = items[cat_index].detail; | |
if ( in_property_array('id', id, cat_details) === false ) { | |
items[cat_index].detail.push( details ); | |
} | |
} else { | |
items.push({ | |
"category": category, | |
"detail": [ details ] | |
}); | |
} | |
console.log(items); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="#" class="reading-list" data-category="Category01" data-id="id01" title="title Text1">Category01 id01</a> <br /> | |
<a href="#" class="reading-list" data-category="Category01" data-id="id02" title="title Text1">Category01 id02</a> <br /> | |
<a href="#" class="reading-list" data-category="Category01" data-id="id03" title="title Text1">Category01 id03</a> <br /> | |
<a href="#" class="reading-list" data-category="Category01" data-id="id04" title="title Text1">Category01 id04</a> <br /> | |
<br /> | |
<a href="#" class="reading-list" data-category="Category02" data-id="id05" title="title Text1">Category02 id05</a> <br /> | |
<a href="#" class="reading-list" data-category="Category02" data-id="id06" title="title Text1">Category02 id06</a> <br /> | |
<a href="#" class="reading-list" data-category="Category02" data-id="id07" title="title Text1">Category02 id07</a> <br /> | |
<a href="#" class="reading-list" data-category="Category02" data-id="id08" title="title Text1">Category02 id08</a> <br /> | |
<br /> | |
<a href="#" class="reading-list" data-category="Category03" data-id="id09" title="title Text1">Category03 id09</a> <br /> | |
<a href="#" class="reading-list" data-category="Category03" data-id="id10" title="title Text1">Category03 id10</a> <br /> | |
<a href="#" class="reading-list" data-category="Category03" data-id="id11" title="title Text1">Category03 id11</a> <br /> | |
<a href="#" class="reading-list" data-category="Category03" data-id="id12" title="title Text1">Category03 id12</a> <br /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment