0

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

4 Answers 4

1

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)

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

3 Comments

Yes, it works perfectly :) Thanks. And what about if I wanted to pick just some values from the array and not others ? In my example, I have just _id and text. But suppose I have also other properties such as photo, slug.... How can I make sure to pick just _id and text but NOT the other properties ?
Could you please clarify the desired output? if you have an array of objects with lots of fields and you need to convert it to list of objects with just 2 fields? e.g. const initialArr = [{id:1, text:'abc', rank:5, etc:'some vale'}, {id:2, text:'rwr', rank:3, etc:'some vale'}, {id:3, text:'36y3563', rank:6, etc:'some vale'}] you can use .map function: initialArray.map(item=> ({id: item.id, text:item.text}))
This is exactly what I needed. You've been very helpful Elivra, many thanks !
1

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

1 Comment

Without using .flat() it works, even though I get arrays into array. But when using .flat(), I log flat is not a function. Thanks @mplungjan
0

Try this below :

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

1 Comment

Thanks @Abhishek Kulkarni :)
0

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

1 Comment

Perfect, it works perfectly @Nick Parsons. You've made my day :) Great explanation

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.