0

I have an array with another nested array I need to map over. I think I almost got it but I am not getting that data back, can only see its an object. Looking at the new index I can see it is looping of the amount of objects in the object array.

Here is what I have currently:

class Menu extends Component {
    constructor(props) {
        super(props);
        const arrayItems = [this.props.items.edges]
        arrayItems.map((currentItem, index) => {
            console.log("The current iteration is: " + index)
            console.log("The current item is: " + currentItem)
            return currentItem.map((newCurrentItem, newIndex) => {
                console.log("The NEW current iteration is: " + newIndex)
                console.log("The NEW current item is: " + newCurrentItem)
                return newCurrentItem
            })
        })

...
}

Here is screenshot of what I can see in my console, which looks promising:

enter image description here

Can someone please point me in correct direction?

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Jan Peeter
  • 347
  • 3
  • 12
  • What's the input? And what's the output you want? – eux Feb 25 '21 at 08:02
  • I think you are iterating an array of objects. if so, objects can't be iterated with `map` – miraj Feb 25 '21 at 08:02
  • @eux I want to sort data alphabetically before using them in state, so this is the step I am trying to achieve before using them in state. Please refer to my other post for more details on this: https://stackoverflow.com/questions/66272670/sort-items-in-state-alphabetically. My question is just if it is possible to map over objects like I am currently trying to do. This is where miraj answer came in handy. Can you please explain more? How would I go about then getting the data out of the newCurrentItem? – Jan Peeter Feb 25 '21 at 08:12

3 Answers3

2

Actually, currentItem is equal to this.props.items.edges in your code, so you could just map this.props.items.edges.

newCurrentItem display as [object Object] is because What does [object Object] mean? (JavaScript).

So you could get data out of CurrentItem as normal.

Your code could be simplified to:

class Menu extends Component {
    constructor(props) {
        super(props);
        const arrayItems = this.props.items.edges
        return arrayItems.map((currentItem) => {
            return currentItem.id // or other attributes you what
        })

...
}
eux
  • 3,072
  • 5
  • 14
0

After I've seen your command with sort items in state alphabetically, I believe map the node value should be what you want.

  const newCurrentItem = [{ node: "abc" }, { node: "def" }];

  // [Object, Object]
  console.log(newCurrentItem);

  // ["abc", "def"]
  console.log(
    newCurrentItem.map((data) => {
      return data.node;
    })
  );

  const newCurrentItem2 = [{ node: { aaa: "bbb" } }, { node: { aaa: "yyy" } }];

  // [Object, Object]
  console.log(newCurrentItem2);

  // ["bbb", "yyy"]
  console.log(
    newCurrentItem2.map((data) => {
      return data.node.aaa;
    })
  );

Code sand box

Vincent Tang
  • 137
  • 8
0

try arrayItems.forEach(currentItem => { //do something }) You can use .map too bt in that case you need to extract the value of any property like @eux said

miraj
  • 546
  • 3
  • 12