4

I need to get object in array for the given index in angular js.

scope.storeList = [{
  'id':101,
  'name':indhu
},{
  'id':102,
  'name':selvin
},{
  'id':103,
  'name':indhu1
}];

In this if i give index value, it should give the object. I tried this below code but could not get:

var list = scope.storeList[2];

Please some one help it.

2
  • angular.js:13424 TypeError: Cannot read property '0' of undefined Commented Oct 26, 2016 at 10:01
  • sure:) But what i have done is correct. it worked after sometimes. Commented Oct 27, 2016 at 10:54

4 Answers 4

2

There is a syntax error in your JSON object: string must be between quotes:

$scope.storeList = [{
  'id':101,
  'name':'indhu'
},{
  'id':102,
  'name':'selvin'
},{
  'id':103,
  'name':'indhu1'
}];

var list = $scope.storeList[2];
console.log(list); // prints Object { id: 103, name: "indhu1" }

Try it on JSFiddle here (don't forget to open console).

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

Comments

1

name values should be in string type.

$scope.storeList = [{
  'id':101,
  'name':"indhu"
},{
  'id':102,
  'name':"selvin"
},{
  'id':103,
  'name':"indhu1"
}];
 console.log("",$scope.storeList[2])

Comments

0

you can simple try:

console.log($scope.storeLit[2]);


$scope.storeList = [{
  'id':101,
  'name':'indhu'
},{
  'id':102,
  'name':'selvin'
},{
  'id':103,
  'name':'indhu1'
}];

console.log(console.log($scope.storeLit[2]);); // prints Object { id: 103, name: "indhu1" }

Comments

0

change indhu and indhu1 to string like

$scope.storeList = [{
  'id':101,
  'name':'indhu'
},{
  'id':102,
  'name':'selvin'
},{
  'id':103,
  'name':'indhu1'
}];

now its proper JSON format and you can get value from list

var list = scope.storeList[2];
console.log(list);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.