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?

3
  • What's the input? And what's the output you want? Commented Feb 25, 2021 at 8:02
  • I think you are iterating an array of objects. if so, objects can't be iterated with map Commented Feb 25, 2021 at 8: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: stackoverflow.com/questions/66272670/…. 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? Commented Feb 25, 2021 at 8:12

3 Answers 3

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
        })

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.