I am having an array as :
const ELEMENT_DATA: any[] =[
{first:'1001', second:'john', third:'India'},
{first:'1002', second:'Mack', third:'US'}]
Now, I want to check if my array is having an element named "first" or not. simply I want to get the names of elements only or just want to check if my array contains the required element or not in angular 8.
I have tried everything like array.includes(arr)
, array.find(arr=>....)
, array.some(...)
etc. But for everything it's giving false value only. even if the element is present then also it's giving false only.
Now, I want to check if my array is having an element named "first" or not
To check whether any of your array items has a key named first
you should use some
const ELEMENT_DATA = [ {first:'1001', second:'john', third:'India'}, {first:'1002', second:'Mack', third:'US'}]; var result = ELEMENT_DATA.some(e => Object.keys(e).includes('first')); console.log(result);
If i understood correctly, you want to check some element of array includes property "first". I could do it like this
const ELEMENT_DATA = [
{first:'1001', second:'john', third:'India'},
{first:'1002', second:'Mack', third:'US'}
];
var result = ELEMENT_DATA.some(e => e.hasOwnProperty('first'));
console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.