js查找对象在数组中的索引,findIndex方法
let objectsArray = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }];
// 假设我们要找到id为2的对象的索引
let targetId = 2;
let index = objectsArray.findIndex(obj => obj.id === targetId);
console.log(index); // 输出: 1
js查找数组是否包含某个值 some方法
let arr = [1,2,3,4,5,6]
let val = 2
let isVal = arr.some(item => item === val);
console.log(isVal)//true
js查找数组中某个对象,并返回该对象 find方法
let items = [
{id: 1, name: 'something'},
{id: 2, name: 'anything'},
{id: 3, name: 'nothing'},
];
let item = items.find(item => {
return item.id == 3;
});
console.log(item) //Object { id: 3, name: "nothing" }
字符串中是否包含数组中的元素
function containsAny(str, arr) {
return arr.some(element => str.includes(element));
}
// 示例使用
const str = "Hello, world!";
const arr = ["Hello", "Hi", "How are you?"];
const result = containsAny(str, arr);
console.log(result); // 输出: true