Separate key and value pairs into two arrays

后端 未结 4 1446
忘了有多久
忘了有多久 2021-01-13 22:48

What would be the best way to go about separating the key and values into two different arrays so that this -

var data = {\"A Key\": 34, \"Another Key\": 16         


        
4条回答
  •  忘掉有多难
    2021-01-13 23:44

    This function will split the data object into a keys and a values array. It returns an object, containing both arrays.

    function splitObj(data){
      var keys = [],
          vals = [];
      for (var l in data) {
       if (data.hasOwnProperty(l){
        keys.push(l);
        vals.push(data[l]];
       }
      }
      return {keys: keys,vals:vals};
    }
    

提交回复
热议问题