JSON jQuery的过滤器的JavaScript数组 [英] json jquery filter javascript array

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

问题描述

我有一个JSON对象数组。我想搜索阵列和每个对象,创建服务这是一个逗号分隔所有这些有是一个值的所有键列表清单。
在服务列表JSON对象列表,然后利用显示在HTML jQuery的每一个。

它的一个大的JSON文件,所以我想尽可能高效地做到这一点成为可能。

我已经正在通过jQuery的访问对象的属性各(即obj.name)
- 所以我想应该是可以过滤使用的每个对象列出的服务
jQuery的过滤器,然后显示键如果值是肯定的。

但它似乎是一个更有效的选择很可能会创建一个新的JavaScript数组,是值加盟服务,然后将该变量添加到HTML之中
追加。

林不知道这将是更快,到目前为止,还没有非常成功的在任...所以任何的建议和示例将是非常有益的。

下面就是JSON数组如下:

  [
 {名:名1,
  服务1:Y,
  服务2:Y,
  服务3:N,
 }, {名:NAME2
  服务1:N,
  服务2:Y,
  服务3:N,
 },
];


解决方案

首先,删除尾随逗号。互联网浏览器变得非常,他们真的很困惑。无论如何,我认为你不想当你说为每个值到搜索的阵列;你想通过数组进行迭代,并解析成一个更可用列表。我建议第一种方法是刚好路过你想要什么,你想要的数组,但如果这是不是一种选择,你要找的是什么的一些这方面的变种,这应该是相当有效(的的jsfiddle例如):

  VAR JSON = [
  {名:名1,服务1:Y,服务2:Y,服务3:N},
  {名:NAME2,服务1:N,服务2:Y,服务3:N}
];
变种解析= {};对于(VAR I = 0,艾朗= json.length; I<艾朗;我++){
  //假设所有我们需要的是名称和列表
  变数名称;
  VAR名单= [];  为(以JSON VAR键[I]){
    VAR值= json的[I] [关键]    //我们需要留住的名称或值y的任何服务
    如果(键===名){
      名称=值;
    }否则如果(价值===Y){
      list.push(键);
    }
  }  //它们添加到阵列解析但是你想
  //我假设你想只列出它们在纯文本
  解析过的[名] = list.join();
}//列出他们在网页上
对(在解析的VAR键){
  的document.write(键+:+解析[键] +< BR>中);
}

在风与一个显示器提供的服务的游客,如有必要还留着一个阵列周围为进一步利用这种方式。

I have a json object array. I want to search the array and for each object, create a list of 'services' that is a comma-seperated list of all the keys which have a value of "yes". The list of json objects with the services list is then displayed in html using jquery's each.

Its a large json file so I want to do it as efficiently as possible.

I already have the object's properties being accessed through jQuery's each (ie, obj.name) -- so I think it should be possible to filter the services listed for each object using jQuery's filter, and then display the key if the value is yes.

But it seems like a more efficient option would probably be to create a new javascript array, join the services with a value of yes and then add that variable to the html being appended.

Im not sure which would be faster and so far havent been very successful at either... so any advice and examples would be very helpful.

Here's what the json array looks like:

[
 {"name":"name1",
  "service1":"y",
  "service2":"y",
  "service3":"n",
 },

 {"name":"name2",
  "service1":"n",
  "service2":"y",
  "service3":"n",
 },
];

解决方案

First off, delete trailing commas. Internet Explorer gets really, really confused by them. Anyway, I assume you don't want to "search" the array when you say "for each value"; you want to iterate through the array and parse it into a more usable list. The first method I'd suggest is just passing what you want as the array you desire, but if that's not an option, what you're looking for is some variant of this, which should be fairly efficient (jsFiddle example):

var json = [
  {"name":"name1", "service1":"y", "service2":"y", "service3":"n"},
  {"name":"name2", "service1":"n", "service2":"y", "service3":"n"}
];
var parsed = {};

for (var i = 0, iLen = json.length; i < iLen; i++) {
  // Assuming all we need are the name and a list
  var name;
  var list = [];

  for (var key in json[i]) {
    var value = json[i][key];

    // We need to hold on to the name or any services with value "y"        
    if (key === "name") {
      name = value;
    } else if (value === "y") {
      list.push(key);
    }
  }

  // Add them to the parsed array however you'd like
  // I'm assuming you want to just list them in plain text
  parsed[name] = list.join(", ");
}

// List them on the web page
for (var key in parsed) {
  document.write(key + ": " + parsed[key] + "<br>");
}

That way you wind up with a display to the visitor of the services available and still keep an array around for further use if necessary.

这篇关于JSON jQuery的过滤器的JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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