zoukankan      html  css  js  c++  java
  • Object对象常用操作

    对象的删除

    let obj = {a:1, b:2, c:3}
    console.log('obj',delete obj.b)
    Object.keys()遍历对象,获取对象的属性名,返回由属性名组成的数组
    let obj = {a:1, c:3}
    console.log(Object.keys(obj))
    // ["a", "c"]
    Object.values()遍历对象,获取属性值,返回属性值组成的数组
    let obj = {a:1, c:3}
    console.log(Object.values(obj))
    // [1, 3]
    obj.hasOwnProperty() 判断对象是否有指定的属性
    let obj = {a:1, c:3}
    console.log(obj.hasOwnProperty('a'))
    // true
    console.log('a' in obj)
    let obj = {a:1, c:3}
    for (key in obj) {
        console.log(key)
    }
    // a c
    
    // 对象合并,以assign的第一个参数为主
    let objOne = {d:4}
    Object.assign(obj, objOne)
    console.log(obj)
    
    // 过滤对象
     let objnew = Object.keys(obj).filter((e) =>{
         console.log(e)
         // a c d
        return obj[e] > 2
    })
    console.log(objnew)
    // ['c', 'd']
    js对象的常用操作
       Object.assign(target,...sources)
        target是目标对象
        sources是源对象
        返回目标对象
    1,解决 复制一个新对象来操作,旧对象不会改变
        var obj = { a: 1 };
        var copy = Object.assign({}, obj);
        copy.a = 2;
        console.log(obj); //a = 1
       2,合并对象属性
        合并的时候如果具有相同属性,会覆盖赋值。
        var ob1 = { a: 1 };
        var ob2 = { b: 2 };
        var o3 = { c: 3 };
        var obj66 = Object.assign({}, ob1, ob2, o3);
        console.log(obj66); //{a:1,b:2,c:3};
    
        // 在对象上面合并 数据
        // let site = Object.assign({}, this.obj, { a: 1, e: 2 })
    3,动态给一个对象,添加数组里的内容
        let objStr = {};
        let arrImg = [11, 22, 33];
        for (let i = 0; i < arrImg.length; i++) {
          objStr["name" + i] = arrImg[i];
        }
        console.log(objStr);
    4,对象合并 数组内容,js对象添加数组
        const dataValue = { aa: 11, bb: 1 };
        const arrImg2 = ["img1", "img2", ""];
        let obj2;
        const setimg = () => {
          //   数组的解构赋值
          const [annexUrlOne, annexUrlTwo, annexUrlThree] = arrImg2 || [];
          const obj1 = { annexUrlOne: "", annexUrlTwo: "", annexUrlThree: "" };
          obj1.annexUrlOne = annexUrlOne;
          obj1.annexUrlTwo = annexUrlTwo;
          obj1.annexUrlThree = annexUrlThree;
          obj2 = Object.assign({}, dataValue, obj1);
        };
        setimg();
        console.log(obj2);

     react 中对象合并数组

    // 对象 添加数组 合成一个新对象
    const [dataValue, setDataValue] = useState({one:001, two:002});
    const [arrImg, setArrImg] = useState([11,222,33]);
    const [annexUrlOne, annexUrlTwo, annexUrlThree] = arrImg || [];
    const params = {
      ...dataValue,
      annexUrlOne,
      annexUrlTwo,
      annexUrlThree,
      headUrl: headImage
    };
    5,对象合并 数组
        // js Object.assign 添加数组
        const arr = [1,2,3]
        let obj00 = {}
        let b = Object.assign({}, obj00, arr)
        console.log(b)
    ES6 对象的解构赋值
        let { foo , bar } = { foo: "aaa", bar: "bbb" };
        // foo = "aaa"
        // bar = "bbb"
    
        // 从函数返回多个值,取值
        function example () {
            return {foo2: 1, bar2: 2}
        }
        let { foo2, bar2 } = example()
    
        // 提取后台返回的JSON数据
        // 解构赋值对提取JSON对象中的数据,尤其有用。
        let jsonDate = { id:42, status:'OK', data:[86, 87] }
        let {id, status, data:number } = jsonDate;
        console.log(id, status, number);
        // 42, "OK", [86, 87]

    参考:https://blog.csdn.net/qq_36838191/article/details/80856656

    参考:

    https://zhuanlan.zhihu.com/p/112519943
    https://www.cnblogs.com/lhl66/p/9505256.html

    你一定要努力,但千万别着急

    https://www.jianshu.com/p/4dca882392b9
    https://www.jianshu.com/p/3bfb8a1ec7ef
     
  • 相关阅读:
    spring
    23种设计模式
    get getline
    ping
    Android四大组件
    C++数据结构
    玩转windows便签
    [JavaScript]再谈 this
    [JavaScript]面向对象编程浅析之XJB讲
    [JavaScript]MVC浅析
  • 原文地址:https://www.cnblogs.com/yizhilin/p/14413559.html
Copyright © 2011-2022 走看看