从JSON数组条件选择 [英] Conditional selecting from a JSON array

查看:191
本文介绍了从JSON数组条件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个数组中显示特定JSON数据。数据是这样的:

I'm trying to select and display specific JSON data within an array. The data looks like this:

{ "thingys" : [
  {
    "type" : "thingy1",
    "text" : "this is thingy1"
  },
  {
    "type" : "thingy2",
    "text" : "this is thingy2"
  },
  {
    "type" : "thingy3",
    "text" : "this is thingy3"
  }
]}

我使用json2html,我通常会使用类似

I'm using json2html, and I would normally use something like

{"tag":"div","html":"${thingys.text}"}

这将是罚款,但我希望能够指定我想$ {} thingys.text其中$ {} thingys.type ==thingy3。我怎么会去这样做呢?

This would be fine, but I want to be able to specify that I want ${thingys.text} where ${thingys.type} == "thingy3". How would I go about doing this?

推荐答案

实现这样的事情将是包括内联函数来处理了thingys像这样的事情最好的办法

best way to achieve something like this would be to include an inline function to process the the "thingys" Something like this

var transforms = {
'main': {"tag":"div","children":function() {

  var out = [];
  for(var i=0; i < this.thingys.length; i++)
     if(this.thingys[i].test == "thingy3") out.push(this.things[i]);

  return( json2html.transform(out,transforms.thing) );
}},

'thing':{"tag":"div","html":"${text}"}
};

这篇关于从JSON数组条件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆