0

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

1 Answer 1

2

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

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.