2017-10-06 45 views
0
var resobj = { 
    "status": { 
     "code": 2000, 
     "message": "Success" 
    }, 
    "order": { 
     "Shop": 1, 
     "Quantity": 1, 
     "Customer": 1 
    } 
} 

我有这样的JSON和我通过订单需要循环和访问。可任何按键(店,数量,顾客)帮助我在此循环通过JSON。中的NodeJS

+0

你是什么意思循环th粗?为了循环你应该有一个订单数组 –

+0

*循环通过订单*我只能看到一个订单节点,循环表明它们有多个。 – gurvinder372

+0

对于你的情况,你将不得不做'for(var key in resobj.order)' – Rajesh

回答

0

使用对象#键将返回键

var resobj = { 
 
    "status": { 
 
     "code": 2000, 
 
     "message": "Success" 
 
    }, 
 
    "order": { 
 
     "Shop": 1, 
 
     "Quantity": 1, 
 
     "Customer": 1 
 
    } 
 
}; 
 

 
Object.keys(resobj.order).forEach(key => console.log(key));

0

的数组,你要寻找的是Object.keys [MDN]

例子:

Object.keys(resobj.order).forEach((key) => { 
    console.log(key); 
}); 
// Will log the keys "Shop", "Quantity" and "Customer"