js经验合集

71 阅读1分钟

01.js 数组点击某个元素,截断到该元素

// 假设有一个数组
let array = ['a', 'b', 'c', 'd', 'e'];
// 加入点击了b,想把b之后的元素都删除
let index = array.indexOf('b');
// 截断索引之后的所有元素
array.splice(index + 1, array.length - index - 1);
// 输出结果
console.log(array); // 输出: ['a', 'b']

js嵌套解构

const person = {
    name: 'Alice',
    age: 25,
    address: {
        city: 'New York',
        country: 'USA'
    }
};

const { address: { city } } = person;
console.log(city); // 输出: New York