简体   繁体   English

Javascript过滤JSON数据

[英]Javascript Filtering JSON data

Sample JSON data:样本 JSON 数据:

{
  "assignments": [{
    "date": "2022-04-01",
    "lName": "lastname",
    "uId": "12345",
    "uCode": "LName1",
    "fName": "FName1 ",
    "aName": "AsignmentName1",
    "aId": "998"
  }]
}

I'd like to filter the following data to get a specific element's contents based on searching for an assignment name.我想根据任务名称的搜索来过滤以下数据以获取特定元素的内容。

For instance in SQL like terms例如在 SQL 类似条款

Select * FROM assignments WHERE `aName` = 'AssignmentName1'

I'm sure this is simple but having trouble with methods for how to accomplish it.我敢肯定这很简单,但是在完成它的方法上遇到了麻烦。

Thanks谢谢

You first have to parse the JSON string:您首先必须解析 JSON 字符串:

const parsedJSON = JSON.parse(jsonString);

The object returned is contains all the data from your JSON string.返回的 object 包含 JSON 字符串中的所有数据。 To access the assignments array you can use dot notation.要访问分配数组,您可以使用点表示法。

const assignments = parsedJSON.assignments;

If you don't need to support old browsers, ES6 has a handy function for finding the value in an object. Use the "find"-function and pass a function that returns true for the item you are looking for:如果您不需要支持旧浏览器,ES6 有一个方便的 function 用于查找 object 中的值。使用“查找”函数并传递一个 function,它会为您要查找的项目返回 true:

const selectedAssignment = assignments.find( (assignment)=> {
  return assignment.aName=="AssignmentName2";
});

If you don't want to use ES6 you can use a for loop.如果你不想使用 ES6,你可以使用 for 循环。

var assignments = JSON.parse(jsonString).assignments;
function getAssignmentWithName(name) {
  for (var i = 0; i < assignments.length; i++) {
    if (assignments[i].aName == name) {
      return assignments[i];
    }
  }
  return false;
}
var selectedAssignment = getAssignmentWithName("AssignmentName1");

I am new here, but if you have access to modern day JavaScript, I would do something like:我是新来的,但如果你可以访问现代 JavaScript,我会做类似的事情:

const data = JSON.parse('{"assignments":[{"date":"2022-04-01","lName":"lastname","uId":"12345","uCode":"LName1","fName":"FName1 ","aName":"AsignmentName1","aId":"998"}]}';
const yourMatch = data.assignments.find(c => c.aName === 'AssignmentName1');
  • Since data.assignments is an array, you can call the find() function on it.由于data.assignments是一个数组,您可以在其上调用find() function。 This functions takes a 'search'-function/lambda as argument.此函数采用“搜索”函数/lambda 作为参数。
  • This search function basically takes an element and decides, whether it is the one you search for, or not aka it returns a boolean.此搜索 function 基本上采用一个元素并决定它是否是您搜索的那个,或者它是否返回 boolean。
  • In my example the arrow function is c => c.aName === 'AssignmentName1' , which is shorter and easier to read than a normal function definition.在我的示例中,箭头 function 是c => c.aName === 'AssignmentName1' ,它比普通的 function 定义更短且更易于阅读。 (You can call c whatever you want, it's just cleaner this way.) (你可以随意拨打c ,这样更干净。)
  • You can exchange find() with filter() , if you accept multiple results and not just the first one.如果您接受多个结果而不仅仅是第一个,则可以将find()filter()交换。

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

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