Last active
October 26, 2021 09:04
-
-
Save Alexander-Pop/77d0eb876764224aa56d002b7dfff105 to your computer and use it in GitHub Desktop.
Find element in an array with JavaScript #javascript #array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let charts = { | |
curationstate: "Curation State", | |
modellingapproach: "Modelling Approach", | |
organism: "Organism", | |
journal: "Journal" | |
}; | |
// Note: the three equal signs so that null won't be equal to undefined | |
if (charts["curationstate"] === undefined) { | |
// do something | |
} | |
// this works even if you have {"publication": undefined} | |
if ("publication" in charts) { | |
// do something | |
} | |
//////////////////////////// | |
var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
console.log(fruits instanceof Array); // the output: true | |
console.log(fruits[0] === undefined); // the output: false | |
//============================================// | |
vendors = [ | |
{ | |
Name: 'Magento', | |
ID: 'ABC' | |
}, | |
{ | |
Name: 'Microsoft', | |
ID: 'DEF' | |
} //and so on goes array... | |
]; | |
if (vendors.filter(e => e.Name === 'Magento').length > 0) { | |
/* true - do sometyhing */ | |
} | |
// or: | |
if (vendors.some(e => e.Name === 'Magento')) { | |
/* true */ | |
} | |
// Compatibility with old browsers: | |
if (vendors.filter(function(e) { return e.Name === 'Magento'; }).length > 0) { | |
/* true - do sometyhing */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment