0

I want to get the value of all price(s).

{
  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"}
}
3
  • Have a look at stackoverflow.com/questions/38148101/… Commented May 14, 2019 at 5:58
  • Can you share what efforts in code you've taken to achieve desired output? Commented May 14, 2019 at 6:03
  • @manishkumar it firebase realtime database json Commented May 14, 2019 at 6:11

5 Answers 5

3

You can use for..in loop to do that.

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); 
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

3

try

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);

Comments

2

Use Object.values() to get the values and get price using . notation

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})

2 Comments

it works to me with little modification. Object.values(a).forEach(e => console.log(e['price']));
What is the issue?
1

Try this solution:

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);

Comments

0

The below code would filter out the price in the result array

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.