我想获取所有价格的值。
{
id: "2019-03",
"-Lc4HDzvL3DTd4aKsdis": {price: 1234, info: "salary", date: "01"},
"-Lc9N5m0N_CJMlD7pa4m": {price: -1212, info: "food", date: "12"},
"-LekFVB-I2jlSb-YHNll": {price: 300000, info: "Actual Expenditure", date: "13"}
}
答案 0 :(得分:3)
您可以使用for..in
循环来做到这一点。
let obj = {id: "2019-03",
-Lc4HDzvL3DTd4aKsdis: {price: 1234, info: "salary", date: "01"},
-Lc9N5m0N_CJMlD7pa4m: {price: -1212, info: "food", date: "12"},
-LekFVB-I2jlSb-YHNll: {price: 300000, info: "Actual Expenditure", date: "13"}};
for(let key in obj) {
if (obj[key].price) {
// do whatever you want with the price here
console.log(obj[key].price);
}
}
答案 1 :(得分:3)
尝试
Object.keys(d).filter(x=>x!='id').map(x=>d[x].price);
let d= {id: "2019-03",
"-Lc4HDzvL3DTd4aKsdis": {price: 1234, info: "salary", date: "01"},
"-Lc9N5m0N_CJMlD7pa4m": {price: -1212, info: "food", date: "12"},
"-LekFVB-I2jlSb-YHNll": {price: 300000, info: "Actual Expenditure", date: "13"}}
let r = Object.keys(d).filter(x=>x!='id').map(x=>d[x].price);
console.log(r);
答案 2 :(得分:2)
使用Object.values()
来获取值并使用.
表示法来获取价格
var a = {
id: "2019-03",
-Lc4HDzvL3DTd4aKsdis: {
price: 1234,
info: "salary",
date: "01"
},
-Lc9N5m0N_CJMlD7pa4m: {
price: -1212,
info: "food",
date: "12"
},
-LekFVB - I2jlSb - YHNll: {
price: 300000,
info: "Actual Expenditure",
date: "13"
}
}
Object.values(a).forEach(e => {typeof(e)=='object'?console.log(e.price):false})
答案 3 :(得分:1)
尝试以下解决方案:
let obj = {
id: "2019-03",
-Lc4HDzvL3DTd4aKsdis: {
price: 1234,
info: "salary",
date: "01"
},
-Lc9N5m0N_CJMlD7pa4m: {
price: -1212,
info: "food",
date: "12"
},
-LekFVB - I2jlSb - YHNll: {
price: 300000,
info: "Actual Expenditure",
date: "13"
}
}
Object.keys(obj).filter(value=>value!='id').map(value=>obj[value].price);
答案 4 :(得分:0)
以下代码将过滤出结果数组中的价格
let obj = {
id: "2019-03",
"-Lc4HDzvL3DTd4aKsdis": {price: 1234, info: "salary", date: "01"},
"-Lc9N5m0N_CJMlD7pa4m": {price: -1212, info: "food", date: "12"},
"-LekFVB-I2jlSb-YHNll": {price: 300000, info: "Actual Expenditure", date: "13"}
};
let result = Object.keys(obj).map((item)=>{
if(obj[item] && obj[item]['price']){
return obj[item]['price'];
}
}).filter(item=>item);
console.log(result);