简体   繁体   English

es6 过滤器 JSON 返回未定义,过滤器对象没有

[英]es6 filter JSON returns undefined, filter object does not

I have a filter function that is returning undefined only when JSON is passed into it.我有一个过滤器函数,只有当 JSON 传入它时才返回 undefined 。 I'd like to use this function to filter objects, JSON or anything really.我想用这个函数来过滤对象、JSON 或任何东西。 Whats the best way to make this work on both objects and JSON?在对象和 JSON 上进行这项工作的最佳方法是什么?

 let a = [{ "employees": { "employee": [{ "id": "1", "firstName": "Tom", "lastName": "Cruise" }, { "id": "2", "firstName": "Maria", "lastName": "Sharapova" }, { "id": "3", "firstName": "James", "lastName": "Bond" }] } }]; var b = [{ name: '', grade: 'x' }, { name: 'yaya', grade: 'x' }, { name: 'x', frade: 'd' }, { name: 'a', grade: 'b' }]; function findIt(arr, searchKey) { return arr.filter(obj => Object.keys(obj).some(key => obj[key].includes(searchKey))); } if (a) { console.log("I found: ", findIt(a, "James")); // breaks } if (b) { console.log("I found: ", findIt(b, "yaya")); // works fine }

You need a reference to the inner array for searching.您需要对内部数组的引用进行搜索。

findIt(a[0].employees.employee, "James"));
//      ^^^^^^^^^^^^^^^^^^^^^^

 let a = [{ employees: { employee: [{ id: "1", firstName: "Tom", lastName: "Cruise" }, { id: "2", firstName: "Maria", lastName: "Sharapova" }, { id: "3", firstName: "James", lastName: "Bond" }] } }]; var b = [{ name: '', grade: 'x' }, { name: 'yaya', grade: 'x' }, { name: 'x', frade: 'd' }, { name: 'a', grade: 'b' }]; function findIt(arr, searchKey) { return arr.filter(obj => Object.keys(obj).some(key => obj[key].includes(searchKey))); } console.log(findIt(a[0].employees.employee, "James")); console.log(findIt(b, "yaya"));

For a deeper find, you could use an recursive approach.要进行更深入的查找,您可以使用递归方法。

 function findIt(object, search) { function find(k) { if (object[k] === search) { return result = object; } return result = findIt(object[k], search); } var result; if (object && typeof object === 'object') { Object.keys(object).some(find); } return result; } var a = [{ employees: { employee: [{ id: "1", firstName: "Tom", lastName: "Cruise" }, { id: "2", firstName: "Maria", lastName: "Sharapova" }, { id: "3", firstName: "James", lastName: "Bond" }] } }], b = [{ name: '', grade: 'x' }, { name: 'yaya', grade: 'x' }, { name: 'x', frade: 'd' }, { name: 'a', grade: 'b' }]; console.log(findIt(a, "James")); console.log(findIt(b, "yaya")); console.log(findIt(a, "foo")); // undefined
 .as-console-wrapper { max-height: 100% !important; top: 0; }

You can try to do a string search.您可以尝试进行字符串搜索。

Logic逻辑

  • Create a copy of object but as string using JSON.stringify创建对象的副本,但使用JSON.stringify作为字符串
  • Create a regex that searches for a pattern :<something>SearchValue .创建一个搜索模式的正则表达式:<something>SearchValue
  • Test object string with this regex and return it in filter使用此正则表达式测试对象字符串并在过滤器中返回它

 let a = [{ employees: { employee: [{ id: "1", firstName: "Tom", lastName: "Cruise" }, { id: "2", firstName: "Maria", lastName: "Sharapova" }, { id: "3", firstName: "James", lastName: "Bond" }] } }]; var b = [{ name: '', grade: 'x' }, { name: 'yaya', grade: 'x' }, { name: 'x', frade: 'd' }, { name: 'a', grade: 'b' }]; function findIt(arr, searchKey) { let reg = new RegExp(':(.*?)' + searchKey, 'g'); return arr.filter(obj => reg.test(JSON.stringify(obj))); } console.log("I found: ", findIt(a, "James")); // breaks console.log("I found: ", findIt(a[0].employees.employee, "James")); // breaks console.log("I found: ", findIt(b, "yaya")); // works fine

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

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