英文:
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: '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'}
],
]
];
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 = '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;
}
}
});
});
}
});
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, 'Dox'));
console.log(find(array, 'foo'));
<!-- end snippet -->
答案2
得分: 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
英文:
Thanks for your question. 3-Dimensional Arrays are interesting
You can use the JavaScript Method filter
to avoid redundant forEach
loops.
Here's my answer:
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;
}
})
})
});
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) => {
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');
答案4
得分: 0
如果一个名称出现多次怎么办?我认为您想要一个包含该名称的每个“secondDepth”数组的索引列表。
如果您想要缩短您的代码,可以查看JS数组方法。例如,您可以在这里使用map、findIndex()和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: '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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论