0

I want to get a value of working day from below object. how can get this ? I'm getting undefined value. i used JSON.parse also, still i'm getting another error.

var timecardDayDetails = [{
    "projectId": 'projectName',
    "activityId": 'projectActivity',
    "location": 'projectLocation',
    "taskDetails": 'projectComments',
    "workingDay": 'Monday',
    "workingDate": "2019-11-04",
    "workingHours": 'workingHours',
    "startTime": 'tsTimeStart',
    "endTime": 'tsTimeEnd',
    "status": "draft"
  },
  {
    "projectId": 'projectName',
    "activityId": 'projectActivity',
    "location": 'projectLocation',
    "taskDetails": 'projectComments',
    "workingDay": 'Monday',
    "workingDate": "2019-11-04",
    "workingHours": 'workingHours',
    "startTime": 'tsTimeStart',
    "endTime": 'tsTimeEnd',
    "status": "draft"
  }
]

var parsedData = JSON.stringify(timecardDayDetails);

alert(parsedData['workingDay']);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rohit Gautam
  • 312
  • 2
  • 17
  • 2
    Your input isn't JSON, just an object literal (which you proceed to stringify for some reason). The `timecardDayDetails` is an array, so you need to access indicies of the array to get to the objects items inside it – CertainPerformance Dec 04 '19 at 09:15
  • 1
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. The only JSON in your question is in the string you're storing in the misnamed `parsedData` variable, because `JSON.stringify` creates a JSON string for the data you pass it. – T.J. Crowder Dec 04 '19 at 09:18

1 Answers1

1

this is array so you need to define index to get values

timecardDayDetails[0] then proporty like timecardDayDetails[0]['workingDay']

var timecardDayDetails = [{
    "projectId": 'projectName',
    "activityId": 'projectActivity',
    "location": 'projectLocation',
    "taskDetails": 'projectComments',
    "workingDay": 'Monday',
    "workingDate": "2019-11-04",
    "workingHours": 'workingHours',
    "startTime": 'tsTimeStart',
    "endTime": 'tsTimeEnd',
    "status": "draft"
  },
  {
    "projectId": 'projectName',
    "activityId": 'projectActivity',
    "location": 'projectLocation',
    "taskDetails": 'projectComments',
    "workingDay": 'Monday',
    "workingDate": "2019-11-04",
    "workingHours": 'workingHours',
    "startTime": 'tsTimeStart',
    "endTime": 'tsTimeEnd',
    "status": "draft"
  }
]



alert(timecardDayDetails[0]['workingDay']);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33