在3维数组中查找索引。

huangapple go评论114阅读模式
英文:

Find the index in the 3 dimensional array

问题

让我们假设我有一个像这样的数组:

  1. const arr = [
  2. [
  3. [
  4. {name: 'Bob'},
  5. {name: 'John'},
  6. ],
  7. [
  8. {name: 'Maria'},
  9. {name: 'Marin'},
  10. {name: 'Marix'},
  11. {name: 'Alex'}
  12. ],
  13. ],
  14. [
  15. [
  16. {name: 'JP'},
  17. {name: 'Dox'},
  18. {name: 'Dor'},
  19. {name: 'Dog'},
  20. ],
  21. [
  22. {name: 'Dol'},
  23. {name: 'Fol'},
  24. {name: 'Fol'},
  25. {name: 'Fol'}
  26. ],
  27. ]
  28. ];

我有一个名字,我想在数组中的对象中找到索引。

这是我的解决方案,它有效,但我想找到另一种解决方案,而不使用3个forEach:

  1. const givenName = 'Dox';
  2. let myIndex;
  3. for (let i = 0; i < arr.length; i++) {
  4. const firstDepth = arr[i];
  5. for (let j = 0; j < firstDepth.length; j++) {
  6. const secondDepth = firstDepth[j];
  7. for (let k = 0; k < secondDepth.length; k++) {
  8. const lastStep = secondDepth[k];
  9. if (lastStep && lastStep.name === givenName) {
  10. myIndex = k;
  11. break;
  12. }
  13. }
  14. if (myIndex !== undefined) {
  15. break;
  16. }
  17. }
  18. if (myIndex !== undefined) {
  19. break;
  20. }
  21. }

谢谢。

英文:

Let's suppose I have an array like this:

  1. const arr = [
  2. [
  3. [
  4. {name: &#39;Bob&#39;},
  5. {name: &#39;John&#39;},
  6. ],
  7. [
  8. {name: &#39;Maria&#39;},
  9. {name: &#39;Marin&#39;},
  10. {name: &#39;Marix&#39;},
  11. {name: &#39;Alex&#39;}
  12. ],
  13. ],
  14. [
  15. [
  16. {name: &#39;JP&#39;},
  17. {name: &#39;Dox&#39;},
  18. {name: &#39;Dor&#39;},
  19. {name: &#39;Dog&#39;},
  20. ],
  21. [
  22. {name: &#39;Dol&#39;},
  23. {name: &#39;Fol&#39;},
  24. {name: &#39;Fol&#39;},
  25. {name: &#39;Fol&#39;}
  26. ],
  27. ]
  28. ];

I have a name and I want to find the index on the object in the array.

There is my solution it works but I want to find another solution without 3 forEach.

  1. const givenName = &#39;Dox&#39;;
  2. let myIndex;
  3. arr.forEach((firstDepth) =&gt; {
  4. if (firstDepth &amp;&amp; Array.isArray(firstDepth)) {
  5. firstDepth.forEach((secondDepth, i) =&gt; {
  6. secondDepth.forEach((lastStep) =&gt; {
  7. if (lastStep) {
  8. const { name } = lastStep;
  9. if (name === givenName) {
  10. myIndex = i;
  11. }
  12. }
  13. });
  14. });
  15. }
  16. });

Thanks.

答案1

得分: 0

You could take a recursive approach and store the indices for each level as return value.

  1. const
  2. find = (array, name) => {
  3. var indices ;
  4. array.some((item, i) => {
  5. if (Array.isArray(item)) {
  6. var temp = find(item, name);
  7. if (temp) return indices = [i, ...temp];
  8. return false;
  9. }
  10. if (item.name === name) return indices = [i];
  11. });
  12. return indices;
  13. },
  14. array = [[[{ name: 'Bob' }, { name: 'John' }], [{ name: 'Maria' }, { name: 'Marin' }, { name: 'Marix' }, { name: 'Alex' }]], [[{ name: 'JP' }, { name: 'Dox' }, { name: 'Dor' }, { name: 'Dog' }], [{ name: 'Dol' }, { name: 'Fol' }, { name: 'Fol' }, { name: 'Fol' }]]];
  15. console.log(find(array, 'Dox'));
  16. console.log(find(array, 'foo'));
英文:

You could take a recursive approach and store the indices for each level as return value.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
find = (array, name) => {
var indices ;
array.some((item, i) => {
if (Array.isArray(item)) {
var temp = find(item, name);
if (temp) return indices = [i, ...temp];
return false;
}
if (item.name === name) return indices = [i];
});
return indices;
},
array = [[[{ name: 'Bob' }, { name: 'John' }], [{ name: 'Maria' }, { name: 'Marin' }, { name: 'Marix' }, { name: 'Alex' }]], [[{ name: 'JP' }, { name: 'Dox' }, { name: 'Dor' }, { name: 'Dog' }], [{ name: 'Dol' }, { name: 'Fol' }, { name: 'Fol' }, { name: 'Fol' }]]];

  1. console.log(find(array, &#39;Dox&#39;));
  2. console.log(find(array, &#39;foo&#39;));

<!-- end snippet -->

答案2

得分: 0

感谢您的问题。3维数组很有趣 在3维数组中查找索引。

您可以使用JavaScript方法filter来避免冗余的forEach循环。

以下是我的回答:

  1. const arr = [
  2. [
  3. [
  4. {name: 'Bob'},
  5. {name: 'John'},
  6. ],
  7. [
  8. {name: 'Maria'},
  9. {name: 'Marin'},
  10. {name: 'Marix'},
  11. {name: 'Alex'}
  12. ],
  13. ],
  14. [
  15. [
  16. {name: 'JP'},
  17. {name: 'Dox'},
  18. {name: 'Dor'},
  19. {name: 'Dog'},
  20. ],
  21. [
  22. {name: 'Dol'},
  23. {name: 'Fol'},
  24. {name: 'Fol'},
  25. {name: 'Fol'}
  26. ],
  27. ]
  28. ];
  29. const givenName = 'Dox';
  30. let myIndex;
  31. _ = arr.filter(firstDepthArray => {
  32. firstDepthArray.filter(secondDepthArray => {
  33. secondDepthArray.filter((thirdDepthObject, i) => {
  34. let name = thirdDepthObject.name;
  35. if (name === givenName) {
  36. myIndex = i;
  37. console.log("Found Index: " + myIndex + " for Element: " + name);
  38. return;
  39. }
  40. })
  41. })
  42. });

问候,

AJ

英文:

Thanks for your question. 3-Dimensional Arrays are interesting 在3维数组中查找索引。

You can use the JavaScript Method filter to avoid redundant forEach loops.

Here's my answer:

  1. const arr = [
  2. [
  3. [
  4. {name: &#39;Bob&#39;},
  5. {name: &#39;John&#39;},
  6. ],
  7. [
  8. {name: &#39;Maria&#39;},
  9. {name: &#39;Marin&#39;},
  10. {name: &#39;Marix&#39;},
  11. {name: &#39;Alex&#39;}
  12. ],
  13. ],
  14. [
  15. [
  16. {name: &#39;JP&#39;},
  17. {name: &#39;Dox&#39;},
  18. {name: &#39;Dor&#39;},
  19. {name: &#39;Dog&#39;},
  20. ],
  21. [
  22. {name: &#39;Dol&#39;},
  23. {name: &#39;Fol&#39;},
  24. {name: &#39;Fol&#39;},
  25. {name: &#39;Fol&#39;}
  26. ],
  27. ]
  28. ];
  29. const givenName = &#39;Dox&#39;;
  30. let myIndex;
  31. _ = arr.filter(firstDepthArray =&gt; {
  32. firstDepthArray.filter(secondDepthArray =&gt; {
  33. secondDepthArray.filter((thirdDepthObject, i) =&gt; {
  34. let name = thirdDepthObject.name;
  35. if (name === givenName) {
  36. myIndex = i;
  37. console.log(&quot;Found Index: &quot; + myIndex + &quot; for Element: &quot; + name);
  38. return;
  39. }
  40. })
  41. })
  42. });

Regards,

AJ

答案3

得分: 0

你好,我也尝试使用递归方法。

  1. findItem(array, name) {
  2. array.forEach((item, index) => {
  3. if (Array.isArray(item)) {
  4. this.findItem(item, name);
  5. } else if (item instanceof Object && item.name === name) {
  6. console.log(`找到项目 ${item.name} 在索引 ${index}`);
  7. }
  8. });
  9. }
  10. this.findItem(arr, 'Alex');
英文:

Hello I also tried with recursive approach.

  1. findItem(array, name) {
  2. array.forEach((item, index) =&gt; {
  3. if (Array.isArray(item)) {
  4. this.findItem(item, name);
  5. } else if (item instanceof Object &amp;&amp; item.name === name) {
  6. console.log(`Found item ${item.name} on index ${index}`);
  7. }
  8. });
  9. }
  10. this.findItem(arr, &#39;Alex&#39;);

答案4

得分: 0

如果一个名称出现多次怎么办?我认为您想要一个包含该名称的每个“secondDepth”数组的索引列表。

如果您想要缩短您的代码,可以查看JS数组方法。例如,您可以在这里使用mapfindIndex()some()的组合:

  1. const arr = [[[{name: 'Bob'}, {name: 'John'}],[{name: 'Maria'}, {name: 'Marin'}, {name: 'Marix'}, {name: 'Alex'}]], [[{name: 'JP'}, {name: 'Dox'}, {name: 'Dor'}, {name: 'Dog'}], [{name: 'Dol'}, {name: 'Fol'}, {name: 'Fol'}, {name: 'Fol'}]]];
  2. const givenName = 'Dox';
  3. const myIndexes = arr.map(secondDepth => Array.isArray(secondDepth)
  4. ? secondDepth.findIndex(thirdDepth => thirdDepth.some(({name}) => name === givenName))
  5. : false
  6. )
  7. .filter(i => i >= 0)
  8. console.log(myIndexes);
英文:

What if one name exists more than once? I think you want a list of indexes for every "secondDepth" -Array that contains the name.

Take a look at JS Array-Methods if u trying to shorten your code. For example you can use a combination of map, findIndex() and some() here:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const arr = [[[{name: &#39;Bob&#39;}, {name: &#39;John&#39;}],[{name: &#39;Maria&#39;}, {name: &#39;Marin&#39;}, {name: &#39;Marix&#39;}, {name: &#39;Alex&#39;}]], [[{name: &#39;JP&#39;}, {name: &#39;Dox&#39;}, {name: &#39;Dor&#39;}, {name: &#39;Dog&#39;}], [{name: &#39;Dol&#39;}, {name: &#39;Fol&#39;}, {name: &#39;Fol&#39;}, {name: &#39;Fol&#39;}]]];
  2. const givenName = &#39;Dox&#39;;
  3. const myIndexes = arr.map(secondDepth =&gt; Array.isArray(secondDepth)
  4. ? secondDepth.findIndex(thirdDepth =&gt; thirdDepth.some(({name}) =&gt; name === givenName))
  5. : false
  6. )
  7. .filter(i =&gt; i &gt;= 0)
  8. console.log(myIndexes);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 17:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定