在Node.js中,可以使用Array.prototype.some()方法來(lái)判斷數(shù)組中是否包含某個(gè)元素。下面是一個(gè)示例代碼:
const arr = [
{ ‘@_android:name’: ‘com.eg.android.AlipayGphone’ },
{ ‘@_android:name’: ‘com.eg.android.AlipayGphoneRC’ },
{ ‘@_android:name’: ‘hk.alipay.wallet’ }
];
const elementToCheck = { ‘@_android:name’: ‘com.eg.android.AlipayGphone’ };
const isElementIncluded = arr.some(item => JSON.stringify(item) === JSON.stringify(elementToCheck));文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-627021.html
console.log(isElementIncluded); // 輸出 true
在上面的代碼中,我們使用了Array.prototype.some()方法來(lái)遍歷數(shù)組中的每個(gè)元素,并使用JSON.stringify()方法將每個(gè)元素轉(zhuǎn)換為字符串進(jìn)行比較。如果找到了與要檢查的元素相等的元素,則返回true,否則返回false。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-627021.html
到了這里,關(guān)于node.js判斷元素是否包括的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!