简体   繁体   English

在数组内查找数组中的元素并返回带有父对象的元素

[英]Find element in array inside array and return element with parent object

Given...鉴于...

    var a = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }];

What is a good way to find the object, for example, with i = 5 plus the object that contains the array that it's in?找到对象的好方法是什么,例如, i = 5加上包含它所在数组的对象?

So, the result would be two references...所以,结果将是两个引用......

var r0 = { "i": 5 };
var r1 = { "b": [{ "i": 4 }, { "i": 5 } ]};

You can do that with forEach :你可以用forEach做到这一点:

 var a = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }]; var r0; var r1; a.forEach(function(item){ item.b.forEach(function(inner){ if(inner.i==5){ r0 = inner; r1 = item; } }); }); console.log(r0); console.log(r1);

Using arrow functions :使用箭头函数

 var a = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }]; var result = a.filter(x => xbfind(b => bi == 5)); console.log(result)

Or using normal functions:或使用正常功能:

 var a = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }]; var result = a.filter(function (x) { return xbfind(function(b) { return bi == 5; }); }); console.log(result);

I think the solution may be different based on your actual background, like which factors you are considering are the most important such as time, space, read/write efficiency etc.我认为根据您的实际背景,解决方案可能会有所不同,例如您考虑的因素是最重要的,例如时间、空间、读/写效率等。

Below are two solutions for basic usage:以下是基本用法的两种解决方案:

 var a = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }] //The commom way: it has to loop whole array for each query queryItem1 = function(searchTarget, searchVal) { return searchTarget.filter( function(val){ return val.b.filter(function(item){return item.i===searchVal;}).length>0; } ) } console.log('---common way----') console.log(queryItem1(a, 5)) console.log('---end----') //Considering `query` efficiency; //build index once, then query by one step instead of loop buildIndexes = function(target) { let indexes = new Object(); for(index in target) { for(itemIndex in target[index].b) { let queryVal = target[index].b[itemIndex].i; let queryIndex = index; if(!indexes[queryVal]) { indexes[queryVal] = []; } indexes[queryVal].push(queryIndex); } } return indexes; } console.log('---consider `query` efficiency way----') indexes = buildIndexes(a) //console.log(indexes) //display index tree console.log('---test case 1----') console.log(indexes[5]) //if query vale is 5, get the index, then call like a[indexes[5][0]] console.log(a[indexes[5][0]]) console.log('---test case 2----') console.log(indexes[6]) //if query vale is 6, get the index, then call like a[indexes[6][0]] console.log(a[indexes[6][0]]) console.log('---end----')

You could take Array#some for both iterations, because if found, the iterations stop by returning true .您可以将Array#some用于两次迭代,因为如果找到,迭代将通过返回true停止。

This proposal returns the first found elements on which the condition is true .此提议返回第一个找到的条件为true元素。

 var array = [{ b: [{ i: 2 }, { i: 3 }] }, { b: [{ i: 4 }, { i: 5 }] }, { b: [{ i: 6 }, { i: 7 }] }], i = 5, r0, r1; array.some(function (a) { return absome(function (b) { if (bi === i) { r0 = b; r1 = a; return true; } }); }); console.log(r0); console.log(r1);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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

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