在3维数组中查找索引。

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

Find the index in the 3 dimensional array

问题

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

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'}
    ],
  ]
];

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

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

const givenName = 'Dox';
let myIndex;

for (let i = 0; i < arr.length; i++) {
  const firstDepth = arr[i];
  for (let j = 0; j < firstDepth.length; j++) {
    const secondDepth = firstDepth[j];
    for (let k = 0; k < secondDepth.length; k++) {
      const lastStep = secondDepth[k];
      if (lastStep && lastStep.name === givenName) {
        myIndex = k;
        break;
      }
    }
    if (myIndex !== undefined) {
      break;
    }
  }
  if (myIndex !== undefined) {
    break;
  }
}

谢谢。

英文:

Let's suppose I have an array like this:

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;}
    ],
  ]
];

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.

const givenName = &#39;Dox&#39;;
let myIndex;
arr.forEach((firstDepth) =&gt; {
  if (firstDepth &amp;&amp; Array.isArray(firstDepth)) {
    firstDepth.forEach((secondDepth, i) =&gt; {
       secondDepth.forEach((lastStep) =&gt; {
        if (lastStep) {
          const { name } = lastStep;
          if (name === givenName) {
             myIndex = i;
          }
        }
      });
    });
  }
});

Thanks.

答案1

得分: 0

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

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' }]]];

console.log(find(array, 'Dox'));
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' }]]];

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

<!-- end snippet -->

答案2

得分: 0

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

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

以下是我的回答:

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'}
    ],
  ]
];

const givenName = 'Dox';
let myIndex;

_ = arr.filter(firstDepthArray => {
  firstDepthArray.filter(secondDepthArray => {
    secondDepthArray.filter((thirdDepthObject, i) => {
      let name = thirdDepthObject.name;
      if (name === givenName) {
        myIndex = i;
        console.log("Found Index: " + myIndex + " for Element: " + name);
        return;
      }
    })
  })
});

问候,

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:

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;}
    ],
  ]
];

const givenName = &#39;Dox&#39;;
let myIndex;

_ = arr.filter(firstDepthArray =&gt; {
  firstDepthArray.filter(secondDepthArray =&gt; {
    secondDepthArray.filter((thirdDepthObject, i) =&gt; {
      let name = thirdDepthObject.name;
      if (name === givenName) {
        myIndex = i;
        console.log(&quot;Found Index: &quot; + myIndex + &quot; for Element: &quot; + name);
        return;
      }
    })
  })
});

Regards,

AJ

答案3

得分: 0

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

findItem(array, name) {
    array.forEach((item, index) => {
        if (Array.isArray(item)) {
            this.findItem(item, name);
        } else if (item instanceof Object && item.name === name) {
            console.log(`找到项目 ${item.name} 在索引 ${index}`);
        }
    });
}

this.findItem(arr, 'Alex');
英文:

Hello I also tried with recursive approach.

findItem(array, name) {
    array.forEach((item, index) =&gt; {
      if (Array.isArray(item)) {
        this.findItem(item, name);
      } else if (item instanceof Object &amp;&amp; item.name === name) {
        console.log(`Found item ${item.name} on index ${index}`);
      }
    });
}

this.findItem(arr, &#39;Alex&#39;);

答案4

得分: 0

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

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

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'}]]];
const givenName = 'Dox';
const myIndexes = arr.map(secondDepth => Array.isArray(secondDepth) 
    ? secondDepth.findIndex(thirdDepth => thirdDepth.some(({name}) => name === givenName))
    : false
  )
  .filter(i => i >= 0)

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 -->

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;}]]];

const givenName = &#39;Dox&#39;;
const myIndexes = arr.map(secondDepth =&gt; Array.isArray(secondDepth) 
    ? secondDepth.findIndex(thirdDepth =&gt; thirdDepth.some(({name}) =&gt; name === givenName))
    : false
  )
  .filter(i =&gt; i &gt;= 0)

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:

确定