我還在學習,前幾天我有一個問題過濾兩個陣列,這對我的理解有很大幫助!
但是現在,我遇到了一個新問題,因為復雜性已經從兩個簡單的陣列增長了一點!現在,我有一個包含多個物件的陣列,命名為動物,它根據動物是寵物、昆蟲還是野生動物對動物進行分類,每個物件都包含一個具有其特征的物件。現在我想在下面看到的一個簡單陣列上進行比較。
帶物件的陣列
let animals = [
{
"housePets" : [ { "name": "Cat", "food" : "Fish", "description": "Long tail, sharp claws" }, { "name": "Dog", "food" : "Biscuits", "description" : "Humans best friend" } ]
},
{
"wildAnimals" : [ { "name": "Giraffe", "food" : "Leafes", "description" : "A very long neck" }, { "name": "Lion", "food" : "Meat", "description" : "Featured in Lion King" } ]
},
{
"insects" : [ { "name": "Ladybug", "food" : "Leafes", "description" : "Red and black" }, { "name": "Spider", "food" : "Flies", "description" : "From the friendly neighbourhood" } ]
}]
編曲:
let animalsArr2 = [housePets]
我在完全理解如何使用這種 JSON 格式時遇到了一些麻煩。對我來說最大的困難是針對特定的陣列,例如 wildAnimals。
例如,我想做的一件事是根據animalsArr2 中的內容過濾animals 陣列,并從animalsarr 中隨機選擇一個在animalsArr2 中沒有特色的陣列,例如“wildAnimals”和“Insects”。我希望有人能給我一些關于如何解決這個問題的想法,或者是否可以以更簡單的方式解決它。
uj5u.com熱心網友回復:
您可以使用.find()陣列方法回傳您想要的串列。例如,如果您想回傳野生動物串列:
let animals = [
{
"housePets" : [
{ "name": "Cat", "food" : "Fish", "description": "Long tail, sharp claws" },
{ "name": "Dog", "food" : "Biscuits", "description" : "Humans best friend" }
]
},
{
"wildAnimals" : [
{ "name": "Giraffe", "food" : "Leafes", "description" : "A very long neck" },
{ "name": "Lion", "food" : "Meat", "description" : "Featured in Lion King" }
]
},
{
"insects" : [
{ "name": "Ladybug", "food" : "Leafes", "description" : "Red and black" },
{ "name": "Spider", "food" : "Flies", "description" : "From the friendly neighbourhood" }
]
}
];
const wildAnimals = animals.find((item) => item.wildAnimals);
上面的代碼基本上檢查你的陣列中的哪個專案有一個名為的屬性wildAnimals并回傳該專案。您可以在此處閱讀有關此方法的更多資訊。
希望有幫助!
uj5u.com熱心網友回復:
我假設您被迫使用這種存盤資料的格式,但我覺得我必須讓您知道這不是存盤此類資訊的最佳方式(您會發現它很難使用它)。你可以有一個這樣的陣列物件,而不是一個物件陣列:
let animals = {
"housePets": [{ "name": "Cat", "food": "Fish", "description": "Long tail, sharp claws" }, { "name": "Dog", "food": "Biscuits", "description": "Humans best friend" }],
"wildAnimals": [{ "name": "Giraffe", "food": "Leafes", "description": "A very long neck" }, { "name": "Lion", "food": "Meat", "description": "Featured in Lion King" }]
}
使用這種方法,讓所有家養動物變得像animals.housePets.
回到你的問題。如果要從中獲取所有家畜,則必須過濾陣列。要過濾陣列,您必須給它一個元素必須通過才能存盤的條件。在這里你可以看到只有家養動物的物件才有housePets屬性,所以我們可以利用它:
let house_animals = animals.filter(obj => obj.housePets)[0].housePets; // [{ "name": "Cat", "food": "Fish", "description": "Long tail, sharp claws" }, { "name": "Dog", "food": "Biscuits", "description": "Humans best friend" }]
它的作用是獲取所有具有“housePets”屬性的物件,獲取第一個(我想永遠只有一個)并讀取屬性,為您提供動物陣列。
要查找僅在 housePets 部分中而不在其他部分中的動物,您可以執行以下操作:
let house_animals = animals.filter(obj => obj.housePets)[0].housePets;
let wild_animals = animals.filter(obj => obj.wildAnimals)[0].wildAnimals;
let insects = animals.filter(obj => obj.insects)[0].insects;
let only_house = house_animals.filter(animal => !wild_animals.includes(animal) && !insects.includes(animal); // This checks if the animal is in the wild_animals or insects array. If it isn't, it keeps it.
你也可以檢查這個問題
希望這有幫助:)
uj5u.com熱心網友回復:
您可以按照此代碼作為起點。更新資料的完整示例如下。
There are two key functions:
function _get(array, label): Take the array and get the names of the animals under the label category.
function _filterAndGet(mainList, filterList): Take the main list of animals, and filter out any animals that are in filter list. Then, pick a random animal from that filtered list.
代碼
const animals = [
{
housePets : [{name: 'Cat', food : 'Fish', description: 'Long tail, sharp claws'}, {name: 'Dog', food : 'Biscuits', description : 'Humans best friend'}]
},
{
wildAnimals : [{name: 'Giraffe', food : 'Leafes', description : 'A very long neck'}, {name: 'Lion', food : 'Meat', description : 'Featured in Lion King'}, {name: 'Cat', food : 'Fish', description: 'Long tail, sharp claws'}, {name: 'Ladybug', food : 'Leafes', description : 'Red and black'}]
},
{
insects : [{name: 'Ladybug', food : 'Leafes', description : 'Red and black'}, {name: 'Spider', food : 'Flies', description : 'From the friendly neighbourhood'}]
}]
const wildAnimals = _get(animals, 'wildAnimals');
console.log(`Wild animals: ${JSON.stringify(wildAnimals)}`);
const housePetsAndInsects = _get(animals, 'housePets').concat(_get(animals, 'insects'));
console.log(`House Pets and Insects: ${housePetsAndInsects}`);
console.log(`Random wild animal not in housePets and insects: ${_filterAndGet(wildAnimals, housePetsAndInsects)}`);
function _filterAndGet(mainList, filterList) {
const filterSet = new Set(filterList);
const filteredMainList = mainList.filter(e => !filterSet.has(e));
const index = Math.floor(Math.random() * filteredMainList.length);
return filteredMainList[index];
}
function _get(array, label) {
const res = [];
for (const elem of array) {
for (const objKey of Object.keys(elem)) {
if (objKey === label) {
for (const item of elem[objKey]) {
res.push(item.name);
}
return res;
}
}
}
return res;
}
示例輸出:
Wild animals: ["Giraffe","Lion","Cat","Ladybug"]
House Pets and Insects: Cat,Dog,Ladybug,Spider
Random wild animal not in housePets and insects: Lion
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429761.html
標籤:javascript 数组 目的 筛选
下一篇:使用實體方法連接串列
