-2

I need to get the title of interval but my function returns undefined.

Function

filterByCategories () {
      return _.orderBy(this.meetups.filter(item => {
        console.log('title: ' + JSON.stringify(item.interval.title, null, 4))

JSON (content of this.meetups):

[
    {
        "id": "-12345",
        "title": "abcds",
        "description": "Hello3",
        "date": "2023-12-29",
        "location": {
            "color": "grey darken-2",
            "icon": "not_interested",
            "id": "none",
            "title": ""
        },
        "creatorId": "abcd1234",
        "time": "24:00",
        "color": {
            "color": "red darken-2",
            "id": "06",
            "title": "Ret"
        },
        "interval": {
            "color": "green darken-2",
            "icon": "local_activity",
            "title": "Weekly",
            "value": 3
        },
        "statusType": false,
        "pause": false,
        "pushNotification": false
    }
]

How to read the title of interval?

Tom
  • 5,588
  • 20
  • 77
  • 129

2 Answers2

0

Since the json is already parsed, you can simply just access it by it's property name. Keep in mind, the data is within an array so there will be multiple .interval.title. Using a for loop is probably a better option here unless you're after a certain index. If you are after a certain index, forget the for loop and just do json[0].interval.name (0 being the index you're after);

const json = [
    {
        "id": "-12345",
        "title": "abcds",
        "description": "Hello3",
        "date": "2023-12-29",
        "location": {
            "color": "grey darken-2",
            "icon": "not_interested",
            "id": "none",
            "title": ""
        },
        "creatorId": "abcd1234",
        "time": "24:00",
        "color": {
            "color": "red darken-2",
            "id": "06",
            "title": "Ret"
        },
        "interval": {
            "color": "green darken-2",
            "icon": "local_activity",
            "title": "Weekly",
            "value": 3
        },
        "statusType": false,
        "pause": false,
        "pushNotification": false
    }
];

for (let i = 0; i < json.length; i++) {
  const meetup = json[i];
  console.log(meetup.interval.title);
}
mwilson
  • 12,295
  • 7
  • 55
  • 95
0

Try this, if there are multiple objects in meetups, you can use Array.map to iterate over the array of objects, and print out the value of the title property within interval?

this.meetups.map(obj => {
  console.log(`title: ${obj['interval']['title']}`);
});

Or simply this, if there is only 1 object in meetups?

this.meetups[0]['interval']['title']

Demo:

const meetups =[{"id":"-12345","title":"abcds","description":"Hello3","date":"2023-12-29","location":{"color":"grey darken-2","icon":"not_interested","id":"none","title":""},"creatorId":"abcd1234","time":"24:00","color":{"color":"red darken-2","id":"06","title":"Ret"},"interval":{"color":"green darken-2","icon":"local_activity","title":"Weekly","value":3},"statusType":false,"pause":false,"pushNotification":false}];

meetups.map(obj => {
  console.log(`title: ${obj['interval']['title']}`);
});

Regarding the code you have provided on your question, I am unsure why did you use Array.filter(), as what it does is that it returns a new array based on the callback function you have provided within filter(). In addition, stringifying it using JSON.stringify() will turn that array into a string, which kind of defeats the purpose of accessing the values within the array of objects.

wentjun
  • 40,384
  • 10
  • 95
  • 107