关于数组的一些常用API(未完待续...)

192 阅读2分钟

set

去除数组的重复成员
    [...new Set(array)]
	const set = new Set([1, 2, 3, 4, 4]);
	[...set]    		// [1, 2, 3, 4]
去除数组重复成员的另一种方法(array from)
    function dedupe(array) {
	    return Array.from(new Set(array));
    }
    dedupe([1, 1, 2, 3]) // [1, 2, 3]

some() --方法测试数组中的某些元素是否通过由提供的函数实现的测试

    var arr2 = ['Prosper', 'Lee', 'is', ['very', 'very'], 'nice', '!', , null];
    arr2.some(function (currentValue, index, array) {
        return currentValue == 'is'; // 通过true
    })

every()---方法测试数组的所有元素是否都通过了指定函数的测试

    var arr2 = ['Prosper', 'Lee', 'is', ['very', 'very'], 'nice', '!', , null];
    arr2.every(function (currentValue, index, array) {
        return currentValue == 'Prosper'; // 不通过false
    })
    var arr3 = ['Prosper','Prosper','Prosper','Prosper','Prosper','Prosper'];
    arr3.every(function (currentValue, index, array) {
        return currentValue == 'Prosper'; // 通过true
    })

filter()---方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素

    var arr4 = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    var result = arr4.filter(word => word.length > 6);
    console.log(result);

Object.keys(obj) -----返回属性名组成新的数组

// simple array
    var obj = { foo: "bar", baz: 42 };
    Object.keys(obj)
    // ["foo", "baz"]

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']

// getFoo is a property which isn't enumerable
var myObj = Object.create({}, {
	getFoo: {
		value: function () { return this.foo; }
 } 
});
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']

Object.values(obj) -----返回属性值组成新的数组

var obj = { foo: "bar", baz: 42 };
Object.values(obj)
// ["bar", 42]

Object.entries()---所有可遍历( enumerable )属性的键值对数组。

var obj = { foo: 'bar', baz: 42 };
Object.entries(obj)
// [ ["foo", "bar"], ["baz", 42] ]

Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象

Object.defineProperty()----修改对象上的现有属性,并返回该对象。

    const object1 = {};
    Object.defineProperty(object1, 'property1', {
      value: 42,
      writable: false
    });
		
	object1.property1 = 77;
    // throws an error in strict mode
    console.log(object1.property1);
    // expected  output: 42

Object.defineProperties()---方法直接在对象上定义新的或修改现有属性,返回该对象

	const object1 = {};
    Object.defineProperties(object1, {
		property1: {
            value: 42,
            writable: true
  		},
		property2: {}
	
    });
	console.log(object1.property1);
    // expected output: 42

Object.entries()----方法[key, value]以与for...in循环提供的顺序相同的顺序返回给定对象自己的可枚举字符串键控属性对的数组(不同之处在于,for-in循环也枚举原型链中的属性)。Object.entries()返回的数组的顺序不依赖于对象的定义方式。如果需要某些排序,则应首先对数组进行排序Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));。

返回数组
	const object1 = {
      a: 'somestring',
      b: 42
    };
	for (let [key, value] of Object.entries(object1)) {
		console.log(`${key}: ${value}`);
    }

    // expected output:
    // "a: somestring"
    // "b: 42"
    // order is not guaranteed
	const obj = { foo: 'bar', baz: 42 };
	console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]

	// array like object
	const obj = { 0: 'a', 1: 'b', 2: 'c' };
	console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]

js数组--拼接数组

使用es6中的 ‘点语法’ 扩展运算符(推荐)
var arr = ['tom', 'jerry'];
var arr2 = [1, 2];

arr.push(...arr2);
console.log(arr)
// ["tom", "jerry", 1, 2]
使用concat(),注意concat()方法生成了一个新的数组,并不改变原来的数组。
var arr = ['tom', 'jerry'];
var arr2 = [1, 2];

var newArr = arr.concat(arr2);
console.log(newArr);
// ["tom", "jerry", 1, 2]