0

I am trying to reduce an array of strings into individual strings, it works well only if i have one string within each array, but not more than one.

const listItems = [
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-1'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-2'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-3'
    }
  }
];

const listIds = listItems.reduce((a, currentValue) =>
            [...a, currentValue.sys.id], [])

// I have also tried `mlistId.toString()`
console.log(...listIds)

Currently i get, for example:

m-list-basic-1 m-list-basic-2 m-list-basic-3 (all on the same line using spread)

m-list-basic-1,m-list-basic-2,m-list-basic-3 (comma'ed using toString()

whereas i want them individual like so:

m-list-basic-1

m-list-basic-2

m-list-basic-3

7
  • What are you trying to do? Commented Sep 17, 2021 at 22:45
  • You are trying to console.log mlistId, but I don't see a variable with that name anywhere? I assume you are trying to console.log listIds Commented Sep 17, 2021 at 22:47
  • Yup, correct and I am trying to return individual strings Commented Sep 17, 2021 at 22:48
  • this looks ok to me (although it would be simpler to understand using map rather than reduce). Can you show both the output you want to obtain, and what you actually get, so we can see the problem? Commented Sep 17, 2021 at 22:49
  • 1
    So, this is really about printing something into the console?? Commented Sep 17, 2021 at 22:54

2 Answers 2

1

Try with forEach:

const listItems = [
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-1'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-2'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-3'
    }
  }
];

const listIds = listItems.reduce((a, currentValue) =>
            [...a, currentValue.sys.id], [])

listIds.forEach(l => console.log(l))

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

Comments

1

If you're trying to print the id as a new line separated string, you don't need reduce to do this. Instead, you want to use Array.map, combined with String.join with a \n character.

See example below:

const listItems = [
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-1'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-2'
    }
  },
  {
    sys: {
      type: 'Link',
      linkType: 'Entry',
      id: 'm-list-basic-3'
    }
  }
];

console.log(listItems.map(item => item.sys.id).join('\n'));

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.