Search code examples
javascriptlodash

fetch Array data based on string value


const a='Piechart'
const b=[ {graph:"Piechart",value: 1},{graph:"Stackedbarchart",value: 1}]

I am comparing a with b and need to get output as value i.e 1 in this case


Solution

  • You can simply do this with Javascript find method

    const a='Piechart'
    const b=[ {graph:"Piechart",value: 1},{graph:"Stackedbarchart",value: 1}]
    
    const res = b.find(obj => obj.graph === a);
    if(res) {
       console.log(res.value);
    }