Vladimir Djukic
Vladimir Djukic

Reputation: 2072

How to assign key to object Angularjs

I am trying to create array like this:

vm.finished_tasks = [];

inside foreach:

(value.finished === 1) ? vm.finished_tasks[item.id].push(value) : '';

There are 2 foreach loop item is from main loop, value is from loop inside...

I want to be able to access from tamlate something like this:

{{ vm.finished_tasks[1] }}

Upvotes: 0

Views: 72

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

In your code

vm.finished_tasks[item.id].push(value)

means value of item.id index is an array and push that value into that array

But you never declared vm.finished_tasks[item.id] as array instead you declared only vm.finished_tasks as an array

If you wanna just print index of vm.finished_tasks

The try like this

vm.finished_tasks.push(value)

But you wanna push data in item.id index

Then try like this

if(!vm.finished_tasks[item.id])
  vm.finished_tasks[item.id]=[];

(value.finished === 1) ? vm.finished_tasks[item.id].push(value) : '';

Upvotes: 1

Related Questions