2016-11-02 24 views
4

jsオブジェクトを歩き、すべてのドットをこのオブジェクトキーのアンダースコアに置き換える必要があります。例えば
jsオブジェクトのキー名のドットをアンダースコアに置き換えてください

{a.a:"test"} 
to 
{a_a:"test"} 

これは私のコードです。

Object.getOwnPropertyNames(match).forEach(function(val, idx, array) { 
    if(val.indexOf(".") != -1){ 
     val.replace(/\./g,'_'); 
    } 
}); 

おかげで、しかし

このような
{ 
"a.a":{ 
    "nee.cc":"sdkfhkj" 
}, 
"b.b": "anotherProp" 
} 

答えて

1

私は、そう単純ではないオブジェクトに問題を抱えているArray.prototype.reduce()を使用して新しいオブジェクトを作成し、単純な文字列置き換えを使用して、小道具の名前にドットを置き換えます

function transformKeys(obj) { 
 
    return Object.keys(obj).reduce(function(o, prop) { 
 
    var value = obj[prop]; 
 
    var newProp = prop.replace('.', '_'); 
 
    o[newProp] = value; 
 
    return o; 
 
    }, {}); 
 
} 
 

 
var result = transformKeys({"a.a":"test", "b.b": "anotherProp"}); 
 

 
console.log(result);

:正規表現なし
0

以下のコードは、この問題を解決するのに役立ちます。

Object.keys(yourObj).forEach(function(v){ 
    yourObj[v.replace(".", "_")] = yourObj[v]; 
    delete yourObj[v]; 
    console.log(yourObj); 
}); 
0

「。」ではなく「_」のキーを持つプロパティを使用して、新しいオブジェクトを作成できます。 :

var transformKeys = obj => { 
    result = {}; 
    Object.keys(obj).forEach(x => { 
    var y = x.replace(".", "_"); 
    result[y] = obj[x]; 
    }); 
    return result; 
} 
console.log(transformKeys({"a.a":"test", "b.b": "anotherProp"})); 
// { a_a: 'test', b_b: 'anotherProp' } 
0

オブジェクトをループしてチェックするオブジェクトを作成して関数を再利用する関数を作成します。 lodashを使用して

var obj = { 
"a.a":{ 
    "nee.cc":"sdkfhkj" 
}, 
"b.b": "anotherProp" 
}; 

d_u(obj); // change dot to underscore function 

function d_u(obj){ 
    for(var i in obj) { 
     if (typeof obj[i] == "object") d_u(obj[i]);    
      obj[i.replace(/\./g,'_')] = obj[i];    
      delete obj[i]; // delete old object [current]  
    } 
} 
console.log(obj); 
0

、ここで再帰的にオブジェクトのキーごとにアンダースコアでドットを交換する関数です。

結果を検証するためのテストを追加しました。

function replaceDotWithUnderscore(obj) { 
 
    _.forOwn(obj, (value, key) => { 
 

 
    // if key has a period, replace all occurences with an underscore 
 
    if (_.includes(key, '.')) { 
 
     const cleanKey = _.replace(key, /\./g, '_'); 
 
     obj[cleanKey] = value; 
 
     delete obj[key]; 
 
    } 
 

 
    // continue recursively looping through if we have an object or array 
 
    if (_.isObject(value)) { 
 
     return replaceDotWithUnderscore(value); 
 
    } 
 
    }); 
 
    return obj; 
 
} 
 

 
// -------------------------------------------------- 
 
// Run the function with a test to verify results 
 
// ------------------------------------------------- 
 

 
var input = { 
 
    "a.a": { 
 
    "nee.cc": "sdkfhkj" 
 
    }, 
 
    "b.b": "anotherProp" 
 
}; 
 

 
var result = replaceDotWithUnderscore(input); 
 

 
// run a quick test to make sure our result matches the expected results... 
 
var expectedResult = { 
 
    "a_a": { 
 
    "nee_cc": "sdkfhkj" 
 
    }, 
 
    "b_b": "anotherProp" 
 
}; 
 

 
console.log(result); 
 
console.assert(_.isEqual(result, expectedResult), 'result should match expected result.');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

関連する問題