2014-04-07 135 views
1

如何用下列對象中的動物/體重鍵/值對創建一個新對象?選擇數據結構中的對象

{ 
    "Pigs": { 
     "Weight": 6.2, 
     "Price": 1.6 
    }, 
    "Chicken": { 
     "Weight": 5.8, 
     "Price": 0.9 
    }, 
    ... 
+0

你能解釋一下自己嗎? :) – justtal

回答

2

試試這個:

var a={ "Pigs": { "Weight": 6.2, "Price": 1.6 }, "Chicken": { "Weight": 5.8, "Price": 0.9 }} 
var g={}; 

for (var o in a) 
    { 
    g[o]=a[o].Weight; 
    } 

console.log(g) //Object {Pigs: 6.2, Chicken: 5.8} 
0

像這樣:

var a = { 
    "pigs" : { "Weight": 6.2, "Price": 1.6 }, 
    "Chicken": { "Weight": 5.8, "Price": 0.9 } 
} 
0

嘗試是這樣的:

var objects = jQuery.parseJSON('{ "Pigs": { "Weight": 6.2, "Price": 1.6 }, "Chicken": { "Weight": 5.8, "Price": 0.9 }}'); 

var animals = []; 

for (object in objects) { 
    animals[object] = objects[object].Weight; 
} 
0

這可能是這樣的:

var animals = {animel1:"Pigs", animal2:"Chicken"}; 
animals.animel1.wight=6.2; 
animals.animel1.price=1,6; 
. 
. 
. 

等等