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];
}