简体   繁体   English

javascript-返回带有唯一子项的父项,该子项与具有嵌套对象的对象数组中的给定搜索字符串匹配

[英]javascript - return parent with only child that matches given search string in array of objects with nested object

This is a follow up question from my previous question . 这是我先前提出的问题的跟进问题 From the answer I've received, I am able to search within nested object in an array of objects. 从收到的答案中,我可以在一组对象的嵌套对象中进行搜索。

Please find this fiddle for example . 请以这个小提琴为例

var data = [
{
  'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
  'asset_count': 2,
  'pdg': 'Invalid',
  'user_area': 'Invalid',
  'deployment_number': 'Invalid',
  'spoc': 'invalid',
  'release': 'Invalid',
  'start_date': '2017-06-12 00:00:00',
  'end_date': '2017-06-16 00:00:00',
  'asset_info': [
    {
      'bams_id': 'BAMS-1001423507',
      'hostname': 'GTVOSS11',
      'status': 10,
      'site_location': 'IEAT01 Tipperary',
      'rack_number': 'VIRTUAL RACK',
      'rack_u_position': 0,
      'manufacturer': 'EMC',
      'model': 'VM',

    },
    {
      'bams_id': 'BAMS-1001368001',
      'hostname': 'None',
      'status': 10,
      'site_location': 'IEAT01 Tipperary',
      'rack_number': 'VIRTUAL RACK',
      'rack_u_position': 0,
      'manufacturer': 'HP',
      'model': 'HP BL460C GEN8',

    }
],
'full_name': 'Invalid (invalid)',
'email_address': 'Invalid'
},
{
  'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11',
  'asset_count': 2,
  'pdg': 'Invalid',
  'user_area': 'Invalid',
  'deployment_number': 'Invalid',
  'spoc': 'invalid',
  'release': 'Invalid',
  'start_date': '2017-06-12 00:00:00',
  'end_date': '2017-06-16 00:00:00',
  'asset_info': [
    {
      'bams_id': 'BAMS-1001423507',
      'hostname': 'GTVOSS11',
      'status': 10,
      'site_location': 'IEAT01 Tipperary',
      'rack_number': 'VIRTUAL RACK',
      'rack_u_position': 0,
      'manufacturer': 'EMC',
      'model': 'VM',

    }
],
'full_name': 'Invalid (invalid)',
'email_address': 'Invalid'
}];

Here when I search for a string 'emc', the function returns two objects which is correct. 在这里,当我搜索字符串'emc'时,该函数返回两个正确的对象。 But 'emc' as 'manufacturer' is in child object. 但是作为“制造商”的“ emc”在子对象中。 And every child object does not satisfies this condition. 并且每个子对象都不满足此条件。 The result I am looking for, for example 'emc', it should return 2 parent objects. 我正在寻找的结果,例如'emc',它应该返回2个父对象。 First parent object should have only one child (the other child has 'hp' as manufacturer). 第一个父对象应该只有一个孩子(另一个孩子的制造商具有“ hp”)。 Second parent object should have one child as it matches the search string. 第二个父对象应该有一个孩子,因为它与搜索字符串匹配。

I tried to make a new object with search result but couldn't make it work. 我试图用搜索结果创建一个新对象,但无法使其正常工作。

How do I return parent object with only child that satisfies the given search string ? 如何返回唯一满足给定搜索字符串的子对象的父对象?

Here is the chatlog on my previous question which can help to understand the problem and requirement. 这是我上一个问题聊天记录 ,可以帮助您理解问题和要求。

You could use an iterative and recursive approach and return the result of the check and build new objects and arrays if the children matches the search value. 您可以使用迭代和递归方法,如果子项与搜索值匹配,则返回检查结果并构建新的对象和数组。

 function getValue(item) { if (Array.isArray(item)) { return item.reduce(iterA, undefined); } if (item && typeof item === 'object') { return iterO(item); } if (typeof item !== 'object' && item.toString().toLowerCase().indexOf(search) !== -1) { return item; } } function iterO(o) { var temp = Object.keys(o).reduce(function (r, k) { var value = getValue(o[k]); if (value) { r = r || {}; r[k] = value; } return r; }, undefined); if (temp) { Object.keys(o).forEach(function (k) { if (!(k in temp)) { temp[k] = o[k]; } }); } return temp; } function iterA(r, a) { var value = getValue(a); if (value) { r = r || []; r.push(value); } return r; } var data = [{ booking_name: "gtec/1101822/lmikdy/ls-rmea/oss11", asset_count: 2, pdg: "Invalid", user_area: "Invalid", deployment_number: "Invalid", spoc: "invalid", release: "Invalid", start_date: "2017-06-12 00:00:00", end_date: "2017-06-16 00:00:00", asset_info: [{ bams_id: "BAMS-1001423507", hostname: "GTVOSS11", status: 10, site_location: "IEAT01 Tipperary", rack_number: "VIRTUAL RACK", rack_u_position: 0, manufacturer: "EMC", model: "VM" }, { bams_id: "BAMS-1001368001", hostname: "None", status: 10, site_location: "IEAT01 Tipperary", rack_number: "VIRTUAL RACK", rack_u_position: 0, manufacturer: "HP", model: "HP BL460C GEN8" }], full_name: "Invalid (invalid)", email_address: "Invalid" }, { booking_name: "gtec/1101822/lmikdy/ls-rmea/oss11", asset_count: 2, pdg: "Invalid", user_area: "Invalid", deployment_number: "Invalid", spoc: "invalid", release: "Invalid", start_date: "2017-06-12 00:00:00", end_date: "2017-06-16 00:00:00", asset_info: [{ bams_id: "BAMS-1001423507", hostname: "GTVOSS11", status: 10, site_location: "IEAT01 Tipperary", rack_number: "VIRTUAL RACK", rack_u_position: 0, manufacturer: "EMC", model: "VM" }], full_name: "Invalid (invalid)", email_address: "Invalid" }], search = 'emc', result = data.reduce(iterA, undefined); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I am unsure how deep your approach need to go, but this should do the trick 我不确定您的方法需要走多深,但这应该可以解决问题

it will make a copy of each row when the child item has it, and replace the set with the matching values only 子项包含子项时,它将复制每一行,并且仅将集合替换为匹配值

 function filterSet(dataSet, matchFn) { return dataSet.reduce((current, row) => { if (typeof row === 'object') { for (let prop in row) { if (Array.isArray(row[prop])) { var set = filterSet(row[prop], matchFn); if (set && set.length > 0) { // copy the row, replace the array property with the results current.push( Object.assign({}, row, { [prop]: set } ) ); // since the full row is now in there // no need to check more break; } } else if (matchFn(row[prop])) { // copy not ref current.push( Object.assign( {}, row ) ); break; } } } return current; }, []); } var data = [{ 'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11', 'asset_count': 2, 'pdg': 'Invalid', 'user_area': 'Invalid', 'deployment_number': 'Invalid', 'spoc': 'invalid', 'release': 'Invalid', 'start_date': '2017-06-12 00:00:00', 'end_date': '2017-06-16 00:00:00', 'asset_info': [{ 'bams_id': 'BAMS-1001423507', 'hostname': 'GTVOSS11', 'status': 10, 'site_location': 'IEAT01 Tipperary', 'rack_number': 'VIRTUAL RACK', 'rack_u_position': 0, 'manufacturer': 'EMC', 'model': 'VM', }, { 'bams_id': 'BAMS-1001368001', 'hostname': 'None', 'status': 10, 'site_location': 'IEAT01 Tipperary', 'rack_number': 'VIRTUAL RACK', 'rack_u_position': 0, 'manufacturer': 'HP', 'model': 'HP BL460C GEN8', } ], 'full_name': 'Invalid (invalid)', 'email_address': 'Invalid' }, { 'booking_name': 'gtec/1101822/lmikdy/ls-rmea/oss11', 'asset_count': 2, 'pdg': 'Invalid', 'user_area': 'Invalid', 'deployment_number': 'Invalid', 'spoc': 'invalid', 'release': 'Invalid', 'start_date': '2017-06-12 00:00:00', 'end_date': '2017-06-16 00:00:00', 'asset_info': [{ 'bams_id': 'BAMS-1001423507', 'hostname': 'GTVOSS11', 'status': 10, 'site_location': 'IEAT01 Tipperary', 'rack_number': 'VIRTUAL RACK', 'rack_u_position': 0, 'manufacturer': 'EMC', 'model': 'VM', }], 'full_name': 'Invalid (invalid)', 'email_address': 'Invalid' } ]; console.log(filterSet(data, (i) => { return i.toString().toLowerCase().indexOf('emc') >= 0; })); console.log(filterSet(data, (i) => { return i.toString().toLowerCase().indexOf('invalid') >= 0; })); 

暂无
暂无

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

相关问题 通过多个匹配条件递归过滤无限嵌套对象数组,但仅返回具有两个匹配实例的父对象 - Recursively filter an array of infinitely nested objects by mutliple matching conditions but only return parent that has an instance of both matches 搜索对象数组,如果值与字符串或子字符串匹配,则返回整个对象 - Search through an array of objects and return whole object if a value matches a string or substring javascript - 在对象数组中搜索字符串并返回数组 - javascript - search for string in array of objects and return array JavaScript 搜索对象数组并返回父对象的索引 - JavaScript search Object Array and return index of parent Javascript - 嵌套的父/子数组递归函数,仅展平父级 - Javascript - Nested parent/child array recursive function that flattens parent only 如何将数组推送到仅匹配父属性的嵌套子对象? - How to push array to nested child object of only matching parent properties? 搜索嵌套的对象数组,并在JavaScript中返回完整的父级作为结果 - Search nested array of objects and return full parents as results in JavaScript 搜索嵌套的对象数组并返回对象的完整路径 - Search nested array of objects and return the full path to the object 搜索对象数组,包括子嵌套对象 - Search Array of Objects including child nested objects 从ID中获取与对象子数组与NodeJS中的猫鼬匹配的父对象 - Get parent object from id that matches objects child array with mongoose in NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM