javascript
如何检查数组是否包含JavaScript中的对象?
In this article, we will look at various methods to check if an array includes an object in JavaScript. Consider the following object,
在本文中,我們將研究各種方法來檢查數組是否包含JavaScript中的對象 。 考慮以下對象,
const squirtle= {name: 'Squirtle-1', HP: 100, type: 'water', favSnack: 'Choco balls' }const pokeArray=['name','location','type'] console.log(squirtle); console.log(pokeArray);Output
輸出量
{name: "Squirtle-1", HP: 100, type: "water", favSnack: "Choco balls"} (3) ["name", "location", "type"]The first method we can use is the includes() method to check if an array contains a property. We can call this method on an array and pass in two parameters, the object name and starting position.
我們可以使用的第一個方法是include()方法,以檢查數組是否包含屬性。 我們可以在數組上調用此方法,并傳入兩個參數,即對象名稱和起始位置。
console.log(pokeArray.includes(squirtle,0))Output
輸出量
falseThe pokeArray array doesn't contain the squirtle object hence it returned "false". The meaning that an array includes an object implies that we're checking inside an array that contains the name of objects and not just properties. So let's create an array of Pokemon,
pokeArray數組不包含squirtle對象,因此返回“ false”。 數組包含對象的含義意味著我們正在檢查包含對象名稱而不只是屬性的數組內部。 因此,讓我們創建一個口袋妖怪數組,
const pokemons = ["charmander", squirtle, "pikachu"]; console.log(pokemons.includes(squirtle, 0));Output
輸出量
trueNow we get true! Let's change our array a bit,
現在我們實現了! 讓我們稍微改變一下數組
const list=["charmander", "pikachu", {name: 'Squirtle-1', HP: 100, type: 'water', favSnack: 'Choco balls' } ] console.log(list);Output
輸出量
(3) ["charmander", "pikachu", {…}]0: "charmander"1: "pikachu"2:HP: 100favSnack: "Choco balls"name: "Squirtle-1"type: "water"__proto__: Objectlength: 3__proto__: Array(0)Our list array contains the whole object inside it. Now we can't use includes() because we don't know the name of the object. We can simply loop through our array and check for each element's type and return true if it was an object.
我們的列表數組包含其中的整個對象。 現在我們不能使用include(),因為我們不知道對象的名稱。 我們可以簡單地遍歷數組并檢查每個元素的類型,如果它是對象則返回true。
list.forEach(l => {if (typeof l == 'object')console.log('Its an object!'); });Output
輸出量
Its an object!We can simplify the above forEach loop by using some() method instead.
我們可以改為使用some()方法來簡化上述forEach循環 。
list.some(value => {return typeof value == 'object'; });Output
輸出量
trueLet's try a few more examples...
讓我們再嘗試一些示例...
const cars=['merc','ferrari',{name: 'Audi'},'tesla'] cars.some(car=>{return typeof car=='object';});Output
輸出量
trueOur cars array represents the names of car brands however it has an object inside it. Sometimes your array might contain objects which shouldn't have been one since they represent the same information that other elements of the array. In those cases, identifying if your array contains an object and then making the data structure consistent would be useful. We could also capture that object and use it somewhere else.
我們的汽車數組代表汽車品牌的名稱,但是其中包含一個對象。 有時,數組可能包含不應該是一個的對象,因為它們表示的信息與數組的其他元素相同。 在這些情況下,識別數組是否包含對象,然后使數據結構一致將很有用。 我們還可以捕獲該對象并在其他地方使用它。
const colors=['red','orange','blue',{date:'12th December',day:'Thursday',year: 2019 } ] var Date; colors.forEach(color=>{if(typeof color=='object')Date=color; }) console.log(Date);Output
輸出量
{date: "12th December", day: "Thursday", year: 2019}In the above example, our colors array had a date object which made no sense so we captured the date object in another variable. This could be useful when you're getting loads of unkempt data from a server and you want to conditionally segregate your data based on what it represents and it's data type.
在上面的例子中,我們的顏色陣列有所以我們捕獲在另一個變量的時間對象,它是沒有意義的日期的對象。 當您從服務器上獲取大量不受歡迎的數據,并且想要根據其表示的內容和數據類型有條件地隔離數據時,這可能很有用。
翻譯自: https://www.includehelp.com/code-snippets/how-to-check-if-an-array-includes-an-object-in-javascript.aspx
總結
以上是生活随笔為你收集整理的如何检查数组是否包含JavaScript中的对象?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Thread类的静态布尔型in
- 下一篇: 摩尔庄园手游大力商人在哪