赞
踩
- const isEqual = (...testObj) => testObj.every((obj) =>
- JSON.stringify(obj) === JSON.stringify(testObj[0]));
-
- console.log(isEqual({ foo: 'bar' }, { foo: 'bar' })); // true
- console.log(isEqual({ foo: 'bar' }, { bar: 'foo' })); // false
- const pluck = (objs, property) => objs.map((obj) => obj[property]);
-
- const result = pluk([{name: 'aa', age: 10}, {name: 'bb', age: 5}], 'age')
-
- console.log(result) //[10, 5]
- const invert = (obj) => Object.keys(obj).reduce((res, k) => Object.assign(res, { [obj[k]]: k }), {});
- // 或
- const invert = (obj) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [v, k]));
-
- invert({ a: '1', b: '2', c: '3' }); // { 1: 'a', 2: 'b', 3: 'c' }
- //方法一
- const removeProptery = (obj) =>
- Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null));
-
- removeProptery ({ a: null, b: undefined, c: 42}); // {c: 42}
-
- // 方法2
- removeObjectNull(sourceObj){
- Object.keys(sourceObj).forEach(item=>{
- if(!sourceObj[item]) delete sourceObj[item]
- })
-
- return sourceObj
- }
- removeObjectNull({ a: null, b: undefined, c: 42})
- const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
-
- // 例子
- diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
- const test = {a: 1, b: 2 }
- if(testObj.hasOwnProperty(a)){
- return true
- }
Object.assign是浅拷贝,源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用。同名属性会替换
- const testObj1 = {a:1, b:2, c:3},
- testObj2 = {d:5, e:7}
- const target = Object.assign(testObj1, testObj2)
-
- console.log(target) //{a:1, b:2, c:3, d:5, e:7}
深拷贝对象
- //直接使用JSON.stringfy()将目标对象转化为字符串形式,然后再转为对象
- let sourceObj = {a: 'a', b: 'c', d: { e: 'g' }}
- let targetObj = JSON.parse(JSON.stringfy(TestObj))
-
- sourceObj.a = 'd'
- sourceObj.d.e = 'j'
-
- console.log(sourceObj ) // {a: 'd', b: 'c', d: { e: 'j' }}
- console.log(targetObj ) // {a: 'd', b: 'c', d: { e: 'j' }}
-
-
- //通过递归的方式实现
- let sourceObj = {
- a: 'a',
- b: 2,
- c: {
- f: 'g',
- h: 'j'
- },
- d: ['e', 'k']
- }
-
- function deepCopy(oldObj){
- let targetObj = {}
- for(let obj in oldObj) {
- let item = oldObj[obj] //获取属性
- //判断是数组?对象?简单类型?
- if(item instanceof Array){
- targetObj[obj] = []
- deepCopy(targetObj[obj], item)
- }else if(item instanceof Object){
- targetObj[obj] = {}
- deepCopy(targetObj[obj], item)
- }else { //简单数据类型,直接赋值
- targetObj[obj] = item
- }
- }
- }
-
-
- const target_obj = deepCopy(sourceObj)
-
- console.log(target_obj )
Object.keys():返回一个数组,成员是参数对象自身的(不含继承的)所有可遍历(enumerable)属性的键名
Object.values:返回一个数组,成员是参数对象自身的(不含继承的)所有可遍历(enumerable)属性的键值
Object.entries:返回一个数组,成员是参数对象自身的(不含继承的)所有可遍历(enumerable)属性的键值对数组
- let testObj = { a: 1, b: 2, c: 3 };
-
- for (let key of Object.keys(testObj )) {
- console.log(key); // 'a', 'b', 'c'
- }
-
- for (let value of Object.values(testObj )) {
- console.log(value); // 1, 2, 3
- }
-
- for (let [key, value] of Object.entries(testObj )) {
- console.log([key, value]); // ['a', 1], ['b', 2], ['c', 3]
- }
- let params = {
- planCode: '',
- orgCode: '0001',
- personCode: '',
- id: '10'
- }
-
- const delPropName = (data, [...args]) => {
- args.forEach(item => {
- delete data[item]
- })
- }
-
- delPropName(params , ['planCode', 'id'])
- console.log(params)
- // {personCode: '', orgCode: '0001'}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。