2016-10-01 152 views
4

我已經轉換從XML JavaScript對象,此爲示範對象:轉換對象

{ 
name: 'current name', 
    attr1: 'attribute1', 
    attr2: 'attribute2', 
    address: { 
    name: 'name1', 
    value: { 
     value: '12' 
    }, 
    attr3: { 
     name: 'no name', 
     attr4: { 
     attr4: 'attribute4' 
     } 
    } 
    }, 
    price: { 
    price: '500' 
    }, 
    in_house: { 
    in_house: '2' 
    } 
} 

我怎麼能轉換成這樣:

{ 
name: 'current name', 
    attr1: 'attr1', 
    address:{ 
    name: 'name1', 
    value: '12', 
    attr3: { 
     name: 'no name', 
     attr4: 'attribute3' 
    } 
    } 
    attr2: 'attr2', 
    price: 500, 
    in_house: 2 
} 

需要轉換所有unusefull對象轉換屬性,例如 { 價: 價: '500' } 成 {價格: '500'}

+1

如果CONSOLE.LOG你的對象,你會看到你的對象是無效的。我建議你在'價格'之前加'',''' –

+0

謝謝@kevinternet – memoris

回答

4

您可以對鍵和它們的值使用迭代遞歸方法。

function moveUp(object, last) { 
 
    var keys = Object.keys(object); 
 

 
    if (keys.length === 1 && keys[0] in last) { 
 
     last[keys[0]] = object[keys[0]]; 
 
     if (last[keys[0]] !== null && typeof last[keys[0]] === 'object') { 
 
      moveUp(last[keys[0]], last); 
 
     } 
 
     return; 
 
    } 
 
    keys.forEach(function (k) { 
 
     if (object[k] !== null && typeof object[k] === 'object') { 
 
      moveUp(object[k], object) 
 
     } 
 
    }); 
 
} 
 

 
var object = { name: 'current name', attr1: 'attribute1', attr2: 'attribute2', address: { name: 'name1', value: { value: '12' }, attr3: { name: 'no name', attr4: { attr4: 'attribute4' } } }, price: { price: '500' }, in_house: { in_house: '2' }, test: { test: { test: { banane: 42 } } } }; 
 

 
moveUp(object); 
 

 
console.log(object); \t
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

如果嵌套單一性能都與parent屬性相同的名稱,則以下應該工作;

var obj = { 
 
name: 'current name', 
 
    attr1: 'attribute1', 
 
    attr2: 'attribute2', 
 
    address: { 
 
    name: 'name1', 
 
    value: { 
 
     value: '12' 
 
    }, 
 
    attr3: { 
 
     name: 'no name', 
 
     attr4: { 
 
     attr4: 'attribute4' 
 
     } 
 
    } 
 
    }, 
 
    price: { 
 
    price: '500' 
 
    }, 
 
    in_house: { 
 
    in_house: '2' 
 
    } 
 
}; 
 

 
for (var prop in obj) typeof obj[prop] === "object" && Object.keys(obj[prop]).length === 1 && (obj[prop] = obj[prop][prop]); 
 
console.log(obj);

1

這裏是一個遞歸函數,將遍歷根對象和每個節點越過它,看看當前節點具有相同名稱的直接子。

const obj = { name: 'current name', attr1: 'attribute1', attr2: 'attribute2', 
 
    address: { name: 'name1', value: { value: '12' }, attr3: { name: 'no name', attr4: { attr4: 'attribute4' }}}, price: { price: '500' }, in_house: { in_house: '2' }} 
 
// helper function to check if a value is an object 
 
const isObject = thing => (
 
    typeof thing !== 'undefined' && 
 
    typeof thing.constructor && 
 
    thing.constructor === Object 
 
) 
 

 
const mutateUselessProperties = (root) => { 
 
    // we need to recursively go through the root object and return it's result 
 
    // after removing properties so we create an inner function for recursion 
 
    const go = (obj) => { 
 
    // if it's just a value return it 
 
    if (!isObject(obj)){ 
 
     return obj 
 
    } 
 
    // it's an object so we loop over the keys 
 
    for (let key in obj) { 
 
     // check if it's an object with a child of the same key 
 
     if (isObject(obj[key]) && obj[key][key]) { 
 
     // reassign the property to it's child of the same name 
 
     obj[key] = obj[key][key] 
 
     } 
 
     // check if it's still an object after possible reassignment 
 
     if (isObject(obj[key])) { 
 
     // it's an object so recrusively go through the child properties 
 
     obj[key] = go(obj[key])  
 
     } 
 
     // may as well check if we are dealing with an array at the same time 
 
     if (Array.isArray(obj[key])) { 
 
     obj[key] = obj[key].map(go)  
 
     } 
 
    } 
 
    // return the current iteration 
 
    return obj 
 
    } 
 
    // run the recursive iteration 
 
    go(root) 
 
    // return the root object that has been mutated 
 
    return root 
 
} 
 

 
console.log(mutateUselessProperties(obj))