i want to create a structure like that:
[{title:'Imágenes', extensions:'jpg,gif,png'}, {title:'Todos', extensions:'*'}]
but i need to create it from a string, in this way (in "js+jquery+php rare mode"):
items = 'Imágenes jpg,gif,png|Todos *'.split('|').each(function() {
list(title, ext) = explode(' ', $this);
filters[] = array('title' => title, 'ext' => ext);
});
i found structures like:
var theStatus = new Object();
function testIt() {
theStatus.Home = 'mouseover';
theStatus['Place'] = 'click';
for (var i in theStatus)
{
alert('theStatus[\''+i+'\'] is ' + theStatus[i])
}
}
and like:
$.alerts = {
verticalOffset: -75,
horizontalOffset: 0,
repositionOnResize: true,
overlayOpacity: .01,
overlayColor: '#FFF'
}
but i cant do nothing functional.
Thanks in advance.
2 Answers 2
This is how I would do it:
items = 'Imágenes jpg,gif,png|Todos *'.split("|").map(function (itemString) {
var parts = itemString.split(" ");
return { title: parts[0], extensions: parts[1] };
});
Or you can do this if you use jQuery and need to support older browsers:
items = $.map('Imágenes jpg,gif,png|Todos *'.split("|"), function () {
var parts = this.split(" ");
return { title: parts[0], extensions: parts[1] };
});
answered Sep 7, 2011 at 9:59
-
You forgot the
.split('|')
on the first line, and the native JavascriptArray.map()
function is only compatible with newer browsers.GregL– GregL2011年09月07日 10:06:37 +00:00Commented Sep 7, 2011 at 10:06 -
Fixed. And it is super easy to add map, reduce etc. to the Array prototype in browsers that don't support them natively. Those methods are so useful, that I would recommend anyone to always do that. There's a project on GitHub called ES5 Shim that does just that.KaptajnKold– KaptajnKold2011年09月07日 10:10:02 +00:00Commented Sep 7, 2011 at 10:10
-
this way works too, and seems to be a bit more cool :) i'll try the "map" way in various browsers so i can see if they support this, anyway, the GregL's way will be a generic solution for me :) thanks from spain!!DavidB– DavidB2011年09月07日 10:13:17 +00:00Commented Sep 7, 2011 at 10:13
-
map doesn't work in IE8 and below. If you need to support that, you can either use the jQuery solution or you can include github.com/kriskowal/es5-shim or you can use GregL's solution.KaptajnKold– KaptajnKold2011年09月07日 11:18:02 +00:00Commented Sep 7, 2011 at 11:18
-
ok, i only need ff, chrome y safari (for the designers), the jquery variant is the best for me, es5-shim is more than i need. In the other hand, i save this question and es5-shim link for the future. i had a problem, now i have a solution. thanks very much again!DavidB– DavidB2011年09月07日 14:16:48 +00:00Commented Sep 7, 2011 at 14:16
Your pseudocode is close.
Try the following vanilla javascript:
var str = 'Imágenes jpg,gif,png|Todos *';
var objs = str.split('|');
var objects = [];
for (var i = 0; i < objs.length; i++) {
var parts = objs[i].split(' ');
objects.push({ title: parts[0], extensions: parts[1] });
}
// objects is now an array of custom objects in the format you specified.
answered Sep 7, 2011 at 10:01
lang-js