JS-judging whether there is an element/key (key) in array/object-existence, enumeration

introduce

This article mainly introduces judging whether there is an element in the array, and returning all elements that meet the conditions; judging whether there is an element that meets the conditions in the Object in the array, and returning all There is a key (key); judge whether there is an element that satisfies the condition in the Object, judge whether there is a key (key) in the Object, and return all the keys (keys) in the Object

1. The elements in the array are number and string

1. Determine whether an element exists in the array

Example: arr=[1,2,3,4];

JS method:
arr.indexOf(3) > -1; // true
arr.indexOf(9) > -1; // false

2. Determine whether there is an element that satisfies the condition in the array, and return all elements that meet the condition

Example: filter all odd numbers in the array: arr=[1, 2, 3, 4, 5, 6, 7, 8, 9];

JS method:
const list = arr.filter(item => item % 2 === 1);
console.log(list); // [ 1, 3, 5, 7, 9 ]

two, object

1. Determine whether there is an element that satisfies the condition in Object

example:

obj = {
    
    name:'李四', sex:'男', age:22};
JS method:
obj.name === '张三'; // true
obj.name === '王五'; // false

2. Determine whether a key (key) exists in the Object

example:

obj = {
    
    name:'张三', sex:'男'};
JS method:
方法1
Object.hasOwnProperty.call(obj,'name'); // true
Object.hasOwnProperty.call(obj,'age'); // false

方法2
obj.hasOwnProperty('name'); // true
obj.hasOwnProperty('age'); // false

方法3'name' in obj; // true
'age' in obj; // false

3. Return all keys (keys) in Object

example:

obj = {
    
    name:'张三', sex:'男'};
JS method:
方法1
Object.getOwnPropertyNames(obj); // [ 'name', 'sex' ]

方法2
Object.keys(obj); // [ 'name', 'sex' ]

3. The elements in the array are object

1. Determine whether there is an element that satisfies the condition in the Object in the array

example:

arr = [
  {
    
    name:'张三', sex:'男', age:11},
  {
    
    name:'李四', sex:'男', age:22},
];
JS method:
arr.some(item => item.name === '张三'); // true
arr.some(item => item.name === '王五'); // false

2. Determine whether there is an element that satisfies the condition in the Object in the array, and return all elements that meet the condition

example:

arr = [
  {
    
    name:'张三', sex:'男', age:11},
  {
    
    name:'李四', sex:'男', age:22},
  {
    
    name:'王五', sex:'男', age:33},
];
JS method:
const list = arr.filter(item => item.age === 11);
console.log(list); //  [{ name: '张三', sex: '男', age: 11 },{ name: '王五', sex: '男', age: 11 }]

3. Determine whether a key (key) exists in the Object in the array

example:

arr = [
  {
    
    name:'张三', sex:'男'},
  {
    
    name:'李四', sex:'男'},
  {
    
    name:'王五', sex:'男'},
];
JS method:
方法1
arr.some(item=>Object.hasOwnProperty.call(item,'name')); // true
arr.some(item=>Object.hasOwnProperty.call(item,'age')); // false

方法2
arr.some(item=>item.hasOwnProperty('name')); // true
arr.some(item=>item.hasOwnProperty('age')); // false

方法3
arr.some(item=>'name' in item); // true
arr.some(item=>'age' in item); // false

------------- The End -------------

License Agreement: For reprinting, please keep the original link and author.

おすすめ

転載: blog.csdn.net/weixin_43937466/article/details/122281563