1、删除数组的重复项
var fruits = [“banana”, “apple”, “orange”, “watermelon”, “apple”, “orange”, “grape”, “apple”];
var uniquefruits = array.from(new set(fruits));
console.log(uniquefruits);
var uniquefruits2 = […new set(fruits)];
console.log(uniquefruits2);
2、替换数组中的特定值
有时在创建代码时需要替换数组中的特定值,有一种很好的简短方法可以做到这一点,咱们可以使用.splice(start、value to remove、valuetoadd),这些参数指定咱们希望从哪里开始修改、修改多少个值和替换新值。
var fruits = [“banana”, “apple”, “orange”, “watermelon”, “apple”, “orange”, “grape”, “apple”];
fruits.splice(0, 2, “potato”, “tomato”);console.log(fruits);
3、置空数组
varfruits=[“banana”,“apple”,“orange”,“watermelon”,“apple”,“orange”,“grape”,“apple”];
fruits.length = 0;console.log(fruits);
returns [];
4、对数组中的所有值求和
js 面试中也经常用reduce方法来巧妙的解决问题
var nums = [1, 5, 2, 6];
var sum = nums.reduce((x, y) => x + y);
console.log(sum);
六安网站推广-六安企业网站要和网站推广结合北京seo优化者浅析网站首页集权的重要性网站制作中完美的动画与图片处理技术易访问性的API将成为未来的主流网站被K的原因有哪些?如何防止被百度人工K站广州网站设计怎样才能吸引到客户?高端网站建设报价科普,告诉你建站成本有哪些掌握SEO技巧,网站关键词排名持续上升