Said951
Said951

Reputation: 31

Access properties of objects into nested Array - Javascript

I'd like please to retrieve all text (text 1,text 2....) from the following array :

[
    {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 1"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 2"
      }
    ]
  },
  {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 3"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 4"
      }
    ]
  },
  {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 5"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 6"
      }
    ]
  }
]  

This array is stored in a variable called stores.

I have tried the following :

const listText = stores.map(count => count.reviews.text // [null, null, null]
const listText = stores.map((count, i) => count.reviews[i].text) // Cannot read property 'text' of undefined
const listText = stores.forEach((key, i) => key.reviews[i].text) // Cannot read property 'text' of undefined

Could you please help me out here, Many thanks

Upvotes: 0

Views: 134

Answers (4)

mplungjan
mplungjan

Reputation: 178413

Map and flat:

const data = [ 
{ "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, 
{ "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, 
{ "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ] 
console.log(
  data.map(items => items.reviews.map(review => review.text)).flat()
)

Upvotes: 1

Nick Parsons
Nick Parsons

Reputation: 50954

Using .flatMap(), you can iterate over each object and then .map() the reviews array of each object to an array of containing the text properties. .flatMap() allows you to flatten all the inner map array results into the one larger resulting array.

const stores = [ { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ];

const res = stores.flatMap((o) => o.reviews.map(({text}) => text));
console.log(res);

If you can't support .flatMap(), you can always map to an array of arrays, using .map() and then flatten it after using .concat() with the spread syntax (...):

const stores = [ { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ];

const res = [].concat(...stores.map((o) => o.reviews.map(({text}) => text)));
console.log(res);

Upvotes: 0

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 1767

Try this below :

let output = [];
            input.map(function (item_1) {
                item_1.reviews.map(function (item_2) {
                  output.push(item_2.text);
                })
            });

Upvotes: 0

elvira.genkel
elvira.genkel

Reputation: 1333

You can't use count.reviews.text as reviews is an array, so you should also iterate through reviews:

const data = [
{
"reviews": 
[
  {
    "_id": "5e84239d6e24358b50f3fe4e",
    "text": "My text 1"
  },
  {
    "_id": "5e8423a46e24358b50f3fe4f",
    "text": "My text 2"
  }
]
  },
  {
"reviews": 
[
  {
    "_id": "5e84239d6e24358b50f3fe4e",
    "text": "My text 3"
  },
  {
    "_id": "5e8423a46e24358b50f3fe4f",
    "text": "My text 4"
  }
]
  },
  {
"reviews": 
[
  {
    "_id": "5e84239d6e24358b50f3fe4e",
    "text": "My text 5"
  },
  {
    "_id": "5e8423a46e24358b50f3fe4f",
    "text": "My text 6"
  }
]
  }
]  

const listText = data.reduce((acc,current) => acc.concat(current.reviews.map(it => it.text)), [])

console.log(listText)

Upvotes: 1

Related Questions