美文网首页
查阅过得一些方法和函数

查阅过得一些方法和函数

作者: 风___________ | 来源:发表于2023-11-13 17:46 被阅读0次

javaScript

以下是其中的几种常见写法:

在JavaScript中,有几种方法可以判断对象是否包含某个属性。

// 1. 使用 `in` 运算符:
const obj = { prop1: value1, prop2: value2 };
const hasProp = 'prop1' in obj; // 返回布尔值

// 2. 使用 `Object.prototype.hasOwnProperty()` 方法:
const obj = { prop1: value1, prop2: value2 };
const hasProp = obj.hasOwnProperty('prop1'); // 返回布尔值

// 3. 使用 `Object.prototype.propertyIsEnumerable()` 方法:
const obj = { prop1: value1, prop2: value2 };
const hasProp = obj.propertyIsEnumerable('prop1'); // 返回布尔值

// 4. 使用 `Object.keys()` 方法:
const obj = { prop1: value1, prop2: value2 };
const hasProp = Object.keys(obj).includes('prop1'); // 返回布尔值

判断数组是否包含某个对象
// 1. 使用 `Array.prototype.includes()` 方法:
const array = [obj1, obj2, obj3];
const containsObj = array.includes(obj); // 返回布尔值
// 2. 使用 `Array.prototype.indexOf()` 方法
const array = [obj1, obj2, obj3];
const containsObj = array.indexOf(obj) !== -1; // 返回布尔值
// 3. 使用 `Array.prototype.find()` 方法
const array = [obj1, obj2, obj3];
const containsObj = array.find(item => item === obj) !== undefined; // 返回布尔值
// 4. 使用 `Array.prototype.some()` 方法:
const array = [obj1, obj2, obj3];
const containsObj = array.some(item => item === obj); // 返回布尔值
// 解构语法+展开表达式更新变量
const { appName, key, mapID, themeID } = this.form;
this.options = { ...this.options, appName, key, mapID, themeID };

// JS
{...(name&&{o_name: name})}
// eg:
let params = {
  ...(this.phone && { phone: this.phone }),
  ...(this.verifyCode && { verifyCode: this.verifyCode }),
  ...(this.name && { name: this.name }),
  ...(this.password && { password: this.password })
};
// 解构赋值
const { name, AGE, PS } = this.form;
const param = [{ name, AGE, PS }];
const { data: { appId, timestamp, nonceStr, signature } } = {data:{appId:"XXX",...},...}
// ...other是剩余参数语法,它将data对象中除了companyName、contact和teleno之外的所有属性收集到一个名为other的新对象中。
const { companyName, contact, teleno,...other } = data 

// 遍历数组
// { type, list: itemList } 是解构模式,它指定了我们要从当前元素中提取哪些属性,并将它们赋给相应的变量
for (const { type, list: itemList } of list) {
    searchModel[type] = itemList;
  }

list.forEach((value, index) => {
  console.log(`Index: ${index}, Value: ${value}`);
});

// 用 for ... in ... 遍历数组遍历的是index
for(const index in list) {
}
// 用展开运算符合并两个对象
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3 };
const mergedObj = { ...obj1, ...obj2 };

vue

<text class="text">{{ item }}</text> # this.item = "sss"

:title_name="navText[before_index]" #  this.navText=["项目", "VIP"] this.before_index = 1

:title_name="is_subscribed?'我的订阅':'订阅设置'" # this.is_subscribed = true

python

out_have_str[:-2]

key = key[:1].upper() + key[1:]
for key, value in my_dict.items():
    print(key, value)


value is not None
value is None
if key not in type_mapping:
  pass
json_data = sys.stdin.read() #  控制台输入多行数据
for index, (key, value) in enumerate(data.items()):
  pass

for item in data:
  pass
with open(f'{time.time()}.xml','w') as file:
  file.write(content)
if not os.path.exists("./folder"):
  pass
template = """
{key}_value_view
View {key}ValueView;
"""
str = template.format(key=key)
def get_person() -> (str, str):
     return "Alice", 25,
person_tuple = get_person()
name, age = person_tuple
name, age = get_person()
my_list = ["apple", "banana", "orange"]
for index, item in enumerate(my_list):
    print(f"Index: {index}, Item: {item}")
for index in range(len(my_list)):
    print(f"Index: {index}, Item: {my_list[index]}")
# 在 f-string 中,如果你想在字符串中插入一个花括号 {},你需要使用两个花括号来进行转义,即 {{}}
"""protected void createdProfileModel({model_file_name} model){{
      listModel =  model;
   }}
"""
import time
time.time
my_string = "Hello, world!"
# 判断字符串是否以指定的前缀开头
result = my_string.startswith("Hello")
print(result)  # 输出: True
result = my_string.startswith("Hi")
print(result)  # 输出: False
# 数组遍历 index + item
for index, module in enumerate(model_pys):
  pass
# 数组拼接为字符串
content_src:str = "\n\n".join(src_strs)

java

Map<String, Integer> hashMap = new HashMap<String, Integer>() {{
            put("Alice", 25);
            put("Bob", 30);
            put("Charlie", 35);
        }};

String[] keys = {"key1", "key2", "key3"};
for (String key : keys) {
}
for (int i = 0; i < keys.length; i++) {
    String key = keys[i];
}

相关文章

  • JS中函数的bind、call、apply总结

    如图,js函数本身就具有一些方法和属性 下面介绍一些常用方法。 1.bind方法 bind方法可以改变函数在被执行...

  • JavaScript函数定义&立即执行

    想要理解立即执行函数,首先需要知道一些函数的基本概念,以下整理了函数定义的方法和立即执行函数的方法。 定义函数 在...

  • 国内网络访问Github资源偏慢解决方法

    查阅到的方法有两个 C语言里的函数 `scanf()` 怎么使用? ```objc #import @interf...

  • jQuery整体结构

    核心函数和一些静态属性 继承方式添加方法

  • 01-Python中函数和方法区别

    一、函数和方法的认知 首先摒弃错误认知:并不是类中的调用都叫方法. 接着上概念 函数 函数是封装了一些独立的功能,...

  • Scala中的函数和方法

    方法是对象中的一个方法函数是一个对象对象就有一些对应的方法 //函数中自带的一些方法(f1是一个函数)scala>...

  • MARK5826 Week 2 lab

    Introduce 前几周lab主要在网站上完成一些基本函数操作,在这里记录一些笔记,便于查阅。 Reading ...

  • chapter 3 类和模块

    3.1 实例的创建 3.2 方法和特殊方法 property()方法,这个函数允许我们创建一些命名特性来替代那...

  • Python学习(二)

    函数 python内置了很多函数可以直接被调用,可以从官方文档查阅内置的函数. 函数调用 调用abs()函数 函数...

  • 继承相关基础知识问答

    一、问题 (一)、继承有什么作用? 通过继承可以继承原有函数的一些属性和方法,避免重复定义一些属性和方法,举个例子...

网友评论

      本文标题:查阅过得一些方法和函数

      本文链接:https://www.haomeiwen.com/subject/nocpvdtx.html