How do I get the attributes from an object array?

xanyi

I have a backbone model variable called companies with an object array type. By putting it to the console I get this:

This object is loaded with the fetch function:

require(["collections/Companies"],
    function(Companies) {
    var companies = new Companies();
    companies.fetch();
    console.log(companies[0].get("name"));

});

companies

companies.js:

define([
    'models/Company'
], function(CompanyModel) {
    'use strict';

    var CompanyCollection = Backbone.Collection.extend({
        model: CompanyModel,
        url: 'scripts/data/companies.json'
    });

    return CompanyCollection;
});

company.js:

define([], function () {
    'use strict';

    var CompanyModel = Backbone.Model.extend({
        defaults: {
            id: '',
            name: '',
            description: ''
        }
    });

    return CompanyModel;
});

I have tried to get the attributes by using get according to the tutorials (console.log(companies[0].get("name"));) with no luck.

What is the correct syntax to get the attributes?

Thanks in advance

Farkhat Mikhalko

To get model from collection you can use Backbone collection method - collection.at(INDEX);

Example code:

var collection = new Backbone.Collection();
collection.add({ id: 1, name: "S"});
collection.add({ id: 2, name: "F"});

console.log(collection.at(0).attributes); // { id: 1, name: "S"}

var model = collection.at(0);

// get attributes from model

console.log(model.get("name")); // "S"

You can play with demo

To get models from collection use : collection.models (you will get array of models)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get the attributes from an object array?

From Dev

How do i get Object index in an array

From Dev

How do I get the index of an object in this array?

From Dev

How do I get the content from the array?

From Dev

How do I get the content from the array?

From Dev

How to get object attributes from a session

From Dev

How get attributes from object php?

From Dev

How do I get the RSA* object from a char array that contains the public key in OpenSSL?

From Dev

How do i get particular value from rest API response array object?

From Dev

How do i get particular value from rest API response array object?

From Dev

How do i get particular value from rest API response array object?

From Dev

How do I vectorize a boolean operation on a numpy array of datetimes where the boolean operation compares datetime object attributes?

From Dev

How do I get specific object's attributes after a related event?

From Dev

How do I get access to keys/attributes of a simple-xml object

From Java

How do i get an array instead of an object using mongoose?

From Dev

How do I get the key for each object in a dynamic JSON array?

From Dev

AngularJS: how do i get data into an object and array?

From Dev

How do I add an object property to the object's attributes?

From Dev

In Javascript, how do I create data- attributes inside options from an object?

From Dev

class method to get array of attributes from managed object subclass?

From Dev

How do I project to remove attributes from an array that don't match a filter

From Dev

How do I remove a WebSocket object from a JavaScript array?

From Dev

How do I filter multiple items from an array and add to an object?

From Dev

How do I extract a specific object from a nested array?

From Dev

how do I toast array values from getter in my object?

From Dev

How can i get recent or closest date from an array object?

From Dev

How can I get a specific value from an array for my object?

From Dev

how can i get an object's value from an array?

From Dev

How can I get data from JSON array object with JQUERY?

Related Related

  1. 1

    How do I get the attributes from an object array?

  2. 2

    How do i get Object index in an array

  3. 3

    How do I get the index of an object in this array?

  4. 4

    How do I get the content from the array?

  5. 5

    How do I get the content from the array?

  6. 6

    How to get object attributes from a session

  7. 7

    How get attributes from object php?

  8. 8

    How do I get the RSA* object from a char array that contains the public key in OpenSSL?

  9. 9

    How do i get particular value from rest API response array object?

  10. 10

    How do i get particular value from rest API response array object?

  11. 11

    How do i get particular value from rest API response array object?

  12. 12

    How do I vectorize a boolean operation on a numpy array of datetimes where the boolean operation compares datetime object attributes?

  13. 13

    How do I get specific object's attributes after a related event?

  14. 14

    How do I get access to keys/attributes of a simple-xml object

  15. 15

    How do i get an array instead of an object using mongoose?

  16. 16

    How do I get the key for each object in a dynamic JSON array?

  17. 17

    AngularJS: how do i get data into an object and array?

  18. 18

    How do I add an object property to the object's attributes?

  19. 19

    In Javascript, how do I create data- attributes inside options from an object?

  20. 20

    class method to get array of attributes from managed object subclass?

  21. 21

    How do I project to remove attributes from an array that don't match a filter

  22. 22

    How do I remove a WebSocket object from a JavaScript array?

  23. 23

    How do I filter multiple items from an array and add to an object?

  24. 24

    How do I extract a specific object from a nested array?

  25. 25

    how do I toast array values from getter in my object?

  26. 26

    How can i get recent or closest date from an array object?

  27. 27

    How can I get a specific value from an array for my object?

  28. 28

    how can i get an object's value from an array?

  29. 29

    How can I get data from JSON array object with JQUERY?

HotTag

Archive