-1

I am trying to create an array containing only id's from the state.words object (below)

MY REQUIRED OUTPUT

['word-1','word-2','word-3','word-4','word-5','word-6','word-7']

My starting data structure looks like this

words: {
    'word-1': { id: 'word-1', content: 'Jimmy Yukka' ,url:'www.paulayling.me'},
    'word-2': { id: 'word-2', content: 'INXS' ,url:'www.paulayling.me'},
    'word-3': { id: 'word-3', content: 'Up North' ,url:'www.paulayling.me'},
    'word-4': { id: 'word-4', content: 'Prince' ,url:'www.paulayling.me'},
    'word-5': { id: 'word-5', content: 'Magic Moose' ,url:'www.paulayling.me'},
    'word-6': { id: 'word-6', content: 'Salt n Pepper' ,url:'www.paulayling.me'},
    'word-7': { id: 'word-7', content: 'Maddonna' ,url:'www.paulayling.me'}
  }

The problem I have is that because it is an object array based methods will not give me the result I need eg below does not work - but I need an equivalent for an object of objects My function is below


  addNewWord() {
    var currentOrder = this.state.words.map(function (book) {
      return book.id
    })

    console.log('words stuff', currentOrder)
  }

I get the error

TypeError: this.state.words.map is not a function

The function is called from the componentDidMount() function if it is relavant

Paul A
  • 387
  • 3
  • 13

1 Answers1

1

map can only be used on arrays. If you want to list the keys in your words object, you can do it this way:

const result = Object.keys(this.state.words);

This will return your required output

HermitCrab
  • 3,194
  • 1
  • 11
  • 10