const array = [
{
id: "1",
children: [],
messages:[1, 'Text'],
entry: "Father",
},
{
id: "2",
entry: "Mother",
children: [
{entry: "John Jr"},
{entry: "Steven Jr"},
{entry: "Tim Jr"},
],
messages:[2, 'Text'],
},
{
id: "3",
entry: "Son",
children: [
{entry: "XXX Jr"},
{entry: "Steven Jr"},
{entry: "Tim Jr"}
],
messages:[3, 'Text'],
},
]
所以我想要的是,當我搜索“蒂姆”時。我希望輸出是
案例 1:當我搜索 Tim 時,我應該得到 id: 2 和 id: 3 的物件,因為 Tim 是兩個物件中都匹配的條目。所以我想以相同的順序回傳整個 id: 2 和 3 物件。
{
id: "2",
entry: "Mother",
children: [
{entry: "John Jr"},
{entry: "Steven Jr"},
{entry: "Tim Jr"},
],
messages:[2, 'Text'],
},
{
id: "3",
entry: "Son",
children: [
{entry: "XXX Jr"},
{entry: "Steven Jr"},
{entry: "Tim Jr"}
],
messages:[3, 'Text'],
}
案例 2:當我搜索 John 時,我應該得到第二個物件
{
id: "2",
entry: "Mother",
children: [
{entry: "John Jr"},
],
messages:[2, 'Text'],
}
案例 3:當我搜索 Steven 時,我應該得到 id:2 和 id:3 作為
id: "2",
entry: "Mother",
children: [
{entry: "John Jr"},
{entry: "Steven Jr"},
],
messages:[2, 'Text'],
},
{
id: "3",
entry: "Son",
children: [
{entry: "XXX Jr"},
{entry: "Steven Jr"},
],
messages:[3, 'Text'],
}
我使用 .filter() 來過濾頂級,但這不是檢查孩子。
array.filter((item) => item.entry.toUpperCase().includes(text.toUpperCase()));
任何幫助將不勝感激,因為我在這里完全迷失了 forEach。提前致謝
uj5u.com熱心網友回復:
您可以使用Array.Filter,
Array.some并將子陣列與搜索entry物件的索引拼接
const array = [{
id: "1",
children: [],
messages: [1, 'Text'],
entry: "Father",
},
{
id: "2",
entry: "Mother",
children: [{
entry: "John Jr"
},
{
entry: "Steven Jr"
},
{
entry: "Tim Jr"
},
],
messages: [2, 'Text'],
},
{
id: "3",
entry: "Son",
children: [{
entry: "XXX Jr"
},
{
entry: "Steven Jr"
},
{
entry: "Tim Jr"
}
],
messages: [3, 'Text'],
},
];
let searchText = 'John';
let result = array.filter(el => {
let i;
let found = el.children.some((e, j) => {
i = j;
return e.entry.toLowerCase().indexOf(searchText.toLowerCase()) >= 0;
});
if (found)
el.children = el.children.splice(0, i 1);
return found;
});
console.log(result);
uj5u.com熱心網友回復:
const data=[{id:"1",children:[],messages:[1,"Text"],entry:"Father"},{id:"2",entry:"Mother",children:[{entry:"John Jr", children: [{entry: 'Bob', children: [{entry: 'Marvin'},{entry: 'Summer', children: [{entry: 'Batman'}, {entry: 'Robin'}]}]},{entry: 'Dave', children: [{entry: 'Sue'}, {entry: 'Sam'}]}]},{entry:"Steven Jr"},{entry:"Tim Jr"}],messages:[2,"Text"]},{id:"3",entry:"Son",children:[{entry:"XXX Jr", children: [{entry: 'Batman'}]},{entry:"Steven Jr"},{entry:"Tim Jr"}],messages:[3,"Text"]}];
function recurseChildren(children) {
const arr = [];
function loop(children) {
for (const entry of children) {
arr.push(entry.entry);
if (entry.children) {
loop(entry.children);
}
}
}
loop(children);
return arr;
}
function finder(data, name) {
// `filter` over the array
return data.filter(obj => {
// Recurse over the children array of all entries
// to compile a list of names
const names = recurseChildren(obj.children);
// For each object check to see if
// at least one of the entries in children
// includes the name
return names.some(entry => {
return entry.includes(name);
});
});
}
console.log(finder(data, 'Tim'));
console.log(finder(data, 'John'));
console.log(finder(data, 'Steven'));
console.log(finder(data, 'Batman'));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366839.html
標籤:javascript 数组 目的
