js数组处理

36 阅读1分钟

... : 展开运算符去重:

const colors = ["red", "orange", "yellow", "green", "orange"];
const onlyColors = [...new Set(colors)];
console.log(onlyColors); // ["red", "orange", "yellow", "green"]

...:切片:

const digitals = ["pc", "watch", "camera", "keyboard", "mouse"];
const [pc, ...otherDigitals] = digitals;
console.log(otherDigitals); // ["watch", "camera", "keyboard", "mouse"]

...:复制数组,可以选择那些不被复制(注意⚠️:浅拷贝,会改变原始值)

const { startDate, endDate, price, ...params } = this.formItem
 
这段代码的目的是将对象 this.formItem 中的 startDate、endDate 和 price 
属性的值分别赋值给变量 startDate、endDate 和 price,同时将剩余的属性和它
们的值收集到一个名为 params 的对象中。这样,你可以在后续的代码中使用这些变量
和 params 对象来访问和操作 this.formItem 中的数据。

也可以复制对象

const student = {
  name: "Jack",
  school: {
    class: "Software Engineering Class 2"
  }
};
const studentCopy = { ...student };
console.log(studentCopy); // {name: "Jack",school:{class: "Software Engineering Class 3"}}