javascript 在 Node.js 中查询 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18577437/
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-27 12:27:11  来源:igfitidea点击:

Querying a JSON object in Node.js

javascriptjsonnode.jsnpm

提问by andyJ

I am quite new to using Node.js. I was looking for a good way to parse and query a JSON object. I have the following JSON object loaded in as a file.

我对使用 Node.js 很陌生。我一直在寻找一种解析和查询 JSON 对象的好方法。我将以下 JSON 对象作为文件加载。

[
{"Key":"Accept","Values":["Application/x-www-form-urlencoded","Application/Json","Application/Xml"]},
{"Key":"Accept-Charset","Values":["UTF-8", "ISO-8859-1"]},
{"Key":"Accept-Encoding","Values":["compress", "gzip"]},
{"Key":"Accept-Language","Values":[]},
{"Key":"Accept-Ranges","Values":[]},
{"Key":"Age","Values":[]},
{"Key":"Allow","Values":[]},
{"Key":"Authorization","Values":["Bearer"]},
{"Key":"Cache-Control","Values":[]},
{"Key":"Connection","Values":[]},
{"Key":"Content-Encoding","Values":[]},
{"Key":"Content-Language","Values":[]},
{"Key":"Content-Length","Values":[]},
{"Key":"Content-Location","Values":[]},
{"Key":"Content-MD5","Values":[]},
{"Key":"Content-Range","Values":[]},
{"Key":"Content-Type","Values":["Application/x-www-form-urlencoded","Application/Json","Application/Xml"]},
{"Key":"Date","Values":[]},
{"Key":"ETag","Values":[]},
{"Key":"Expect","Values":[]},
{"Key":"Expires","Values":[]},
{"Key":"From","Values":[]},
{"Key":"Host","Values":[]},
{"Key":"If-Match","Values":[]},
{"Key":"If-Modified-Since","Values":[]},
{"Key":"If-None-Match","Values":[]},
{"Key":"If-Range","Values":[]},
{"Key":"If-Unmodified-Since","Values":[]},
{"Key":"Last-Modified","Values":[]},
{"Key":"Max-Forwards","Values":[]},
{"Key":"Pragma","Values":[]},
{"Key":"Proxy-Authenticate","Values":[]},
{"Key":"Proxy-Authorization","Values":[]},
{"Key":"Range","Values":[]},
{"Key":"Referer","Values":[]},
{"Key":"TE","Values":[]},
{"Key":"Trailer","Values":[]},
{"Key":"Transfer-Encoding","Values":[]},
{"Key":"Upgrade","Values":[]},
{"Key":"User-Agent","Values":[]},
{"Key":"Via","Values":[]},
{"Key":"Warning","Values":[]}
]

I want to be able to find a Key by value and return the values array.

我希望能够按值找到一个键并返回值数组。

So for example how do I find the values where the key is equal to Content-Type.

例如,我如何找到键等于Content-Type.

Thanks in advance for your help

在此先感谢您的帮助

采纳答案by Andy

My comment notwithstanding, I would loop through the array like so:

尽管有我的评论,但我会像这样循环遍历数组:

function searchByKey(key) {
  for (var i = 0, l = arr.length; i < l; i++){
    if (arr[i]['Key'] === key) {
      return arr[i]['Values'];
    }
  }
  return false;
}

回答by Andrew Duthie

Since you're using Node.js, you can take advantage of the newer Array.prototype.filter

由于您使用的是 Node.js,因此您可以利用较新的Array.prototype.filter

var myData = require('./data.json'),
  myFilteredData = myData.filter(function(obj) {
    return obj.key === 'Content-Type';
  });