JS 1 slice, 2 split, 3 include

1 slice is use for slice a string
2 split is the same as python, with limit
3 include is the same with startswith in python
4 map and filter is used for functions inside
5 concat is used for concat arrays

// slice(startindex, endindex), not include endindex
var A = 'Geeks for Geeks'; 
  
b = A.slice(0,5); 
c = A.slice(6,9); 
d = A.slice(10); 

console.log(b);
console.log(c);
console.log(d);

// split is the same as python, but has limit as start index
function func() { 
  //Original string 
  var str = 'Geeks for Geeks'
  var array = str.split("for"); 
  console.log(array);
  var array = str.split("for", 1); 
  console.log(array); 
  var check = str.includes("Geeks");
  console.log(check);
  var check = str.includes("Geeks", 1);
  console.log(check);
  var check = str.includes("Geeks", 12);
  console.log(check);
} 
func();

const arr = [1, 2, "3", "4", 5, 6, "7", 8, "9"];
// Converting string to number in array
const arrAsNumbers = arr.map(el => Number(el));
console.log( arrAsNumbers );
const arrStr = ["1", "2", "3"];
const arrAsNumber = arrStr.map(el => parseInt(el));
console.log( arrAsNumber);

function even(el) {
    return el % 2 === 0;
}

// Filtering even numbers
// const filteredArr = arrAsNumbers.filter(function (el) {
//     return even(el);
// });
// or
const filteredArr = arrAsNumbers.filter(even);
  
console.log(filteredArr);   // [ 2, 4, 6, 8 ]

function funcConcat() {
    var num1 = [11, 12, 13],
        num2 = [14, 15, 16],
        num3 = [17, 18, 19];
    console.log(num1.concat(num2, num3));
}

funcConcat();

var alpha = ['a', 'b', 'c'];
console.log(alpha.concat(1, [2, 3]));

var num1 = [23];
var num2 = [89, [67]];
console.log(num1.concat(num2));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值