Rajveer Parjapati
Rajveer Parjapati

Reputation: 34

how to display value from json in angularjs

i have json array like following

var data = [
    {
        id: 1,
        name: "abx",
    },{
        id:2,
        name: "silver"
    }
];

is this possible in angularjs. i can display name of json according to assign value like

var x = 2;

{{data.x.name}}

i mean if have id of row is 2 then i just want to show {{name}} directly not want to add looping code and match id == record.id

Upvotes: 0

Views: 452

Answers (1)

tymeJV
tymeJV

Reputation: 104805

Directly in dot notation? No. You can write a method that takes an id, then returns the name from that:

$scope.getNameFromId(data, id) {
    var item = data.filter(function(item) {
        return item.id === id
    });

    return item.length ? item[0].name : "N/A";
}

Then in your view:

{{getNameFromId(data, 2)}}

Upvotes: 2

Related Questions