0

I have an array that looks like this:

[Object{ 82893u82378237832={ id=8, no=1, type="event", name="Sample1"}}, Object{ 128129wq378237832={ id=9, no=1, type="event", name="Sample2"}} ]

Now ignoring the first part which is just a random token i want to get the array inside that. i.e. id,no,type,name and display them in an ng-repeat, how can i achieve this?

This is how i create the above array:

var obj = {};
obj[data.responseData] = {
    id: 8,
    no: 1,
    type: 'event',
    name: ''
}

$scope.items.push(obj);
4
  • your format is invalid... Commented Apr 20, 2015 at 15:06
  • Sorry if it is i didn't know how to copy the full array from my console so i just typed it. Commented Apr 20, 2015 at 15:08
  • That is not an array, but an object. Commented Apr 20, 2015 at 15:10
  • Oh yes, potato potato :) Commented Apr 20, 2015 at 15:12

1 Answer 1

1

Your example doesn't include an array.

Anyway here's a good example.

Use map to transform an array to another array and use ng-repeat in a similar way that I've done.

Html:

<div ng-app="app">
    <div ng-controller="ctrl"> 
        <div ng-repeat="item in myArr">
            {{item}}
        </div>
    </div>
</div>

JS:

angular.module('app', []).
controller('ctrl', function ($scope) {
    var arr = [{
        myId: '82893u82378237832',
        id: 8,
        no: 1,
        type: "event",
        name: "Sample1"
    }, {
        myId: '128129wq378237832',
        id: 9,
        no: 1,
        type: "event",
        name: "Sample2"
    }];

    // mapped the new object without myId
    $scope.myArr = arr.map(function (item) {
        return {
            id: item.id,
            no: item.no,
            type: item.type,
            name: item.name
        }
    });
});

JSFIDDLE.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay the thing is i want to be able to remove each object form the array at a click of a button that is why i added the key, how would i achieve that with your way? :)
Can you give me a concrete example?
I mean i want to remove/delete a specific object in that array in an ng-repeat
If you want to delete it then you can use the delete keyword as in this example. jsfiddle.net/4cwep2h0 . If you want to hide it from display you should use the ng-if or ng-hide directive on the specific element. In the first example I just added a json display, but you can do it the way you want to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.