-1

Duplicate of: map function for objects (instead of arrays)

How do i map or iterate through an object where the values is another object as such:

[{
    "id":2,
    "name":"Jane Smith",
    "position":"Cook",
    "applied":"02/08/16",
    "experience":4,
    "availability":{
        "M":1,
        "T":1,
        "W":1,
        "Th":1,
        "F":0,
        "S":0,
        "Su":0
    },
    "questions": [{
        "text":"Have you ever been convicted of a felony?",
        "answer":"Yes"
    }]
},
...(2 other objects with the same format)....
]

I need to access the availability object

azrahel
  • 1,143
  • 2
  • 13
  • 31
Berheet
  • 79
  • 2
  • 11
  • It's a bit unclear what behavior you are looking for. Perhaps you could provide an example output, or some pseudocode illustrating what you're trying to do. – jered Sep 20 '18 at 23:16
  • Tell us what you want as a final output. `map or iterate through an object` can mean a lot of things. – Mark Sep 20 '18 at 23:16
  • @jered currently I am mapping through the array with these 3 objects in them and rendering them to the screen, however I cannot render the availability section and questions section because they are formatted different – Berheet Sep 20 '18 at 23:25
  • @Berheet Are you asking how to convert the structure of the _availability_ objects inside the array? – FK82 Sep 21 '18 at 07:01

3 Answers3

0

I assume you want a list of availability objects.

const availabilityObjects = objects.map(object => object.availability)

Will do it. Code snippet follows:

const objects =  [{
   "id": 2,
   "name": "Jane Smith",
   "position": "Cook",
   "applied": "02/08/16",
   "experience": 4,
   "availability": {
    "M": 1,
    "T": 1,
    "W": 1,
    "Th": 1,
    "F": 0,
    "S": 0,
    "Su": 0
   },
   "questions": [{
    "text": "Have you ever been convicted of a felony?",
    "answer": "Yes"
   }]
  },
  {
   "id": 2,
   "name": "Jane Smith",
   "position": "Cook",
   "applied": "02/08/16",
   "experience": 4,
   "availability": {
    "M": 1,
    "T": 1,
    "W": 1,
    "Th": 1,
    "F": 0,
    "S": 0,
    "Su": 0
   },
   "questions": [{
    "text": "Have you ever been convicted of a felony?",
    "answer": "Yes"
   }]
  },
  {
   "id": 2,
   "name": "Jane Smith",
   "position": "Cook",
   "applied": "02/08/16",
   "experience": 4,
   "availability": {
    "M": 1,
    "T": 1,
    "W": 1,
    "Th": 1,
    "F": 0,
    "S": 0,
    "Su": 0
   },
   "questions": [{
    "text": "Have you ever been convicted of a felony?",
    "answer": "Yes"
   }]
  }
 ]
 
 console.log(objects.map(object => object.availability))
FK82
  • 4,907
  • 4
  • 29
  • 42
danday74
  • 52,471
  • 49
  • 232
  • 283
0

Beware that there is no guarantee of the order of an object's keys, so there is no perfectly consistent way to iterate over them. However, you can use the for...in statement to iterate over the enumerable properties of an object. You can use that to basically iterate through they key/value pairs of an objects and do something with them.

const availability = {
    "M":1,
    "T":1,
    "W":1,
    "Th":1,
    "F":0,
    "S":0,
    "Su":0
};
for (const key in availability) {
    console.log(key, availability[key]);
}

// Output:
/*
  M 1
  T 1
  W 1
  Th 1
  F 0
  S 0
  Su 0
*/

Since it is unclear exactly how you want to use the data I can't provide any more detail than that but it should get you started.

jered
  • 11,220
  • 2
  • 23
  • 34
0

The map() equivalent of object is Object.keys() and Object.entries() It is a feature introduced in ES7

    const data = [{
    "id":2,
    "name":"Jane Smith",
    "position":"Cook",
    "applied":"02/08/16",
    "experience":4,
    "availability":{
        "M":1,
        "T":1,
        "W":1,
        "Th":1,
        "F":0,
        "S":0,
        "Su":0
    },
    "questions": [{
        "text":"Have you ever been convicted of a felony?",
        "answer":"Yes"
    }]
}
 ]

    console.log(Object.entries(data[0].availability));
    console.log(Object.keys(data[0].availability));

Check this here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys For your data if you have more than one object then you can first iterate through the array first using array.map() and use Object.entries() inside map()

FK82
  • 4,907
  • 4
  • 29
  • 42
Divyansh
  • 41
  • 5
  • You might want to change the first sentence. `Array.map` is not equivalent to `Object.keys` or `Object.entries` (they do different things). In fact there is no equivalent method to `Array.map` on `Object` at all. Only something like `o.entries().map(f).reduce((result, [key, value]) => { result[key] = value; return result}, {})` would be roughly equivalent in that it maps the elements and returns a new object. – FK82 Sep 21 '18 at 06:56
  • @FK82 By equivalent I mean to say you can traverse array by using map() method and if you want to traverse object then you can refer Object.keys() and Object.entries(). – Divyansh Sep 21 '18 at 12:00
  • I understand what you're trying to say. The way you express it is not correct though, the semantics of `Array.map` are the following: _"creates a new array with the results of calling a provided function on every element in the calling array."_ ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)). Neither `Object.keys` nor `Object.entries` do that on their own. That's why I'm saying that they are not equivalent. You can combine them with other functions to create something comparable to `Array.map` for objects though. – FK82 Sep 21 '18 at 13:04