在3维数组中找到索引

时间:2020-01-03 09:53:22

标签: javascript arrays

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

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;
arr.forEach((firstDepth) => {
  if (firstDepth && Array.isArray(firstDepth)) {
    firstDepth.forEach((secondDepth, i) => {
       secondDepth.forEach((lastStep) => {
        if (lastStep) {
          const { name } = lastStep;
          if (name === givenName) {
             myIndex = i;
          }
        }
      });
    });
  }
});

谢谢。

4 个答案:

答案 0 :(得分:0)

您可以采用递归方法,并将每个级别的索引存储为返回值。

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'));

答案 1 :(得分:0)

感谢您的提问。 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

答案 2 :(得分: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(`Found item ${item.name} on index ${index}`);
      }
    });
}

this.findItem(arr, 'Alex');

答案 3 :(得分:0)

如果一个名字不止一次存在怎么办?我认为您想要包含该名称的每个“ secondDepth” -Array的索引列表。

如果您想缩短代码,请查看JS Array-Methods。例如,您可以在此处使用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);