json jquery 过滤器 javascript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6347137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:21:35  来源:igfitidea点击:

json jquery filter javascript array

javascriptjqueryarraysjsonfilter

提问by sharpiemarker1

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.

我有一个 json 对象数组。我想搜索数组,并为每个对象创建一个“服务”列表,该列表是一个逗号分隔的列表,其中包含所有值为“是”的键。带有服务列表的 json 对象列表然后使用 jquery 的 each 显示在 html 中。

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

它是一个很大的 json 文件,所以我想尽可能高效地完成它。

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.

我已经通过 jQuery 的 each(即 obj.name)访问了对象的属性——所以我认为应该可以使用 jQuery 的过滤器过滤为每个对象列出的服务,如果值为是,则显示键。

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.

但似乎更有效的选择可能是创建一个新的 javascript 数组,加入值为 yes 的服务,然后将该变量添加到要附加的 html 中。

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:

这是 json 数组的样子:

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

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

回答by CtrlShiftF11

If you just want to filter the array then use grep.

如果您只想过滤数组,请使用 grep。

grep - Finds the elements of an array which satisfy a filter function. The original array is not affected.

grep - 查找满足过滤器函数的数组元素。原数组不受影响。

http://api.jquery.com/jQuery.grep/

http://api.jquery.com/jQuery.grep/

回答by brymck

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):

首先,删除尾随逗号。Internet Explorer 对它们非常非常困惑。无论如何,我假设您在说“为每个值”时不想“搜索”数组;您想遍历数组并将其解析为更可用的列表。我建议的第一种方法是将你想要的作为你想要的数组传递,但如果这不是一个选项,你正在寻找的是它的一些变体,这应该是相当有效的(jsFiddle 示例):

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.

这样,您最终会向访问者展示可用服务的内容,并在必要时保留一个数组以供进一步使用。

回答by zod

jQuery.inArray() Search for a specified value within an array and return its index (or -1 if not found).

jQuery.inArray() 在数组中搜索指定值并返回其索引(如果未找到,则返回 -1)。

http://api.jquery.com/jQuery.inArray/

http://api.jquery.com/jQuery.inArray/

Or

或者

http://api.jquery.com/jQuery.each/

http://api.jquery.com/jQuery.each/