0

I am trying to extract the data in an array, however, it seems I am not doing it right, but when i console log it, I can see all the data. I want to be able to extract only all the titles in the array. Kindly show me where I am getting it wrong.

      const movies = [
       {
         title: 'Show stopper',
         rate: 5,
         runtime: 120,
         information: 'Show stopper is available',
       },
       {
         title: 'Drive through',
         rate: 233,
         runtime: 65,
         information: 'Drive through is not available',
       },
      ];

        componentDidMount() {
          this.getItemValues();
        }

        getItemValues = () => {
          const { title, rate } = movies;

          console.log(title, rate);
        }
2
  • Can you share the context of this code? Is it inside of a class/component? Commented Nov 20, 2019 at 21:50
  • 4
    Your movies variable is an array with objects in it. Your destructuring declaration indicates that you want to get the values of the properties "title" and "rate", but the array has no such properties. The objects in the array do, but you'd have to iterate through the array and do something with each object individually. Commented Nov 20, 2019 at 21:52

4 Answers 4

1

Movies is an array not, just one object with the keys title and rate. You could iterate through the elements of the array using a for loop or a forEach. For example:

getItemValues = () => {
  movies.forEach((element) => console.log(element.title, element.rate));
}

That would print out all your titles and rates. You are close to doing what you want, just keep in mind the type of what you are sending into variables and functions.

To answer your question: you could

let titles = [];
getItemTitles = () {
   movies.forEach((movie) => titles.push(movie.title);)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Pointy is right.

You can't destructure an array like that.

You'd need to iterate over your array and store the values.

movies.map(e => {   
    return {
    title: e.title, 
    rate: e.rate 
    }
})

Comments

0

Read up on Array.prototype.map - this without a doubt the easiest way to achieve your objective:

const movies = [{
  title: 'Show stopper',
  rate: 5,
  runtime: 120,
  information: 'Show stopper is available',
}, {
  title: 'Drive through',
  rate: 233,
  runtime: 65,
  information: 'Drive through is not available',
}];

const titles = movies.map(movie => movie.title);

console.log(titles)

Therefore, using this approach, your React code would look something like this:

componentDidMount() {
    this.getItemValues();
}

getItemValues = () => {
    const titles = movies.map(movie => movie.title);
    console.log(titles);
}

FYI: Object destructuring and Array destructuring, while similar, use different syntaxes and should be used properly. In your code, you are attempting to destructure an array using object destructuring syntax which will not work as you expect).

Comments

0

You can try this:

componentDidMount() {
    const titles = this.getItemValues();
    console.log(titles);
}

getItemValues = () => movies.map(movie => movie.title);

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.