当前位置:网站首页>JS to find and update the specified value in the object through the key

JS to find and update the specified value in the object through the key

2022-06-24 08:48:00 Front end tripod

Preface

stay js Most of the data values of object types in obj.a.b.c.d… In this way, the value is taken , However, sometimes the data levels are nested very deeply, and it is very inconvenient to take values every time , So simply encapsulate two small methods for getting and updating values .

1、 adopt key Get the specified value in the object

      /**
       *  adopt key Get the specified value of the object 
       * @param {*} obj  Object of value 
       * @param {*} objKey   After splicing key data ,string ‘>’ Symbol splicing 
       * @return {*}  Return found value 
       */
      const getObjDataByKey = (obj, objKey) => {
        const keyList = objKey.split('>');
        return keyList.reduce((pre, item) => pre[item], obj);
      }

      const data = {
        a: {
          b: {
            c: {
              d: [1,2,3]
            }
          }
        }
      }
      console.log(getObjDataByKey(data, 'a>b>c>d'));

The above code passes in two parameters ( Raw data and through > Spliced key value ), And then by intercepting > obtain key Of list data , Finally through keyList Of reduce Method gets one layer at a time obj Data in , Until finally get the last specified value and return . The printing effect is as follows :

 Insert picture description here

2、 adopt key to update obj Specified data in

      /**
       *  adopt key to update obj Specified data in 
       * @param {*} obj  Objects that update values 
       * @param {*} objKey    After splicing key data ,string ‘>’ Symbol splicing 
       * @param {*} newValue   Updated value 
       * @return {*}  Return the updated data 
       */

      const updateObjDataByKey = (obj, objKey, newValue) => {
        const keyList = objKey.split('>');
        const lasteKey = keyList[keyList.length - 1];
        keyList.reduce((pre, item) => {
          if (item === lasteKey) pre[item] = newValue;
          return pre[item];
        }, obj);

        return obj;
      }

      const data = {
        a: {
          b: {
            c: {
              d: [1,2,3]
            }
          }
        }
      }
      
      console.log(updateObjDataByKey(data, 'a>b>c>d', [555555555555]));


The above code passes in three parameters ( Raw data 、 adopt > Spliced key value 、 The latest value ), First by intercepting > obtain key Of list data , Finally through keyList Of reduce Method gets one layer at a time obj Data in , Judge when loop to keyList The last level will newValue Assigned to the current data , Finally, return the passed in object ( The intermediate assignment takes advantage of the characteristics of the reference data type ). The printing effect is as follows :

 Insert picture description here

原网站

版权声明
本文为[Front end tripod]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240606102688.html