简体   繁体   中英

Dynamic Value in json array using Angularjs

So I have an empty array defined.

admin.links = [];

I then push items to it like so.

angular.forEach(links, function(value, key) {

                var title = value.title;
                var url = value.url;

                admin.links.counter.push({
                    'parent' : counter,
                    'name' : title,
                    'url' : url
                })

            })

When I run the code above I get an error

Cannot read property 'push' of undefined

counter is a dynamic value. How would I do this?

I want it to be something like admin.links.0

What you mean with:

counter is a dynamic value.

admin.links is an array, so if you want to add items you must use:

admin.links.push

if, instead, you want links to be an object you should initialize it with:

admin.links = {
  counter: []
}

admin.links.counter.push()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM