简体   繁体   English

如何创建复杂的javascript结构

[英]how to create a complex javascript structure

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"): 但我需要以这种方式从字符串创建它(在“js + jquery + php罕见模式”中):

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. 提前致谢。

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: 或者,如果您使用jQuery并且需要支持旧版浏览器,则可以执行此操作:

items = $.map('Imágenes jpg,gif,png|Todos *'.split("|"), function () {
    var parts =  this.split(" ");
    return { title: parts[0], extensions: parts[1] };
});

Your pseudocode is close. 你的伪代码很接近。

Try the following vanilla javascript: 尝试以下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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM