2

I do have an object which is containing some field, three of them are arrays. And I would like to map over them in one go.

Let me show how I am doing it right now:

containerNames = this.state.fileContent.xContainers.map(xContainer => <Container containerName={xContainer} />);

containerNames.push(this.state.fileContent.yContainers.map(yContainer => <Container containerName={yContainer} />));

containerNames.push(this.state.fileContent.zContainers.map(zContainer => <Container containerName={zContainer} />));

For me it does seems like it could be made differently. With one lambda?

Ahmed Kamal
  • 2,660
  • 3
  • 21
  • 36
Suule
  • 2,197
  • 4
  • 16
  • 42

6 Answers6

3

The code in OP will make something like [1, 2, 3, [4, 5, 6], [7, 8, 9]], which I hope is not what you want. If you want [1, 2, 3, 4, 5, 6, 7, 8, 9], use this:

const containerNames = ["xContainers", "yContainers", "zContainers"].
    flatMap(field => this.state.fileContent[field].map(...));

If you want [[1, 2, 3], [4, 5, 6], [7, 8, 9]], then change the flatMap into map.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Nice, I'd do that in a chain rather than nested: `[x,y,z].flatMap(f => fileContent[f]).map(name => etc` – georg Jun 14 '19 at 08:41
2

You're looking to create an array of arrays, so use a .map nested inside another:

const { xContainers, yContainers, zContainers } = this.state.fileContent;
containerNames = [xContainers, yContainers, zContainers].map(
  containers => containers.map(container => <Container containerName={container} />)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1
const {fileContent : {xContainers, yContainers, zContainers}} = this.state;

const mergedArray = [
   ...xContainers,
   ...yContainers,
   ...zContainers
]

const containerNames = mergedArray.map(container => <Container containerName={container} />)

bkm412
  • 1,047
  • 5
  • 11
1
containerNames = [...xContainers, ...yContainers, ...zContainers].map(
  containers => containers.map(container => <Container containerName={container} />)
);
Agam kumar
  • 11
  • 2
1

In the mapToContainer function pass the c argument to your <Container containerName={c} />

const allow = ['xContainers', 'yContainers', 'zContainers']
const fileContent = {
  xContainers: [{name: '1'}, {name: '2'}, {name: '3'}],
  yContainers: [{name: '1'}, {name: '2'}, {name: '3'}],
  zContainers: [{name: '1'}, {name: '2'}, {name: '3'}],
  foo: {}
}

const mapToContainer = containers => containers.map(c => c.name)

const containerNames = 
  Object.keys(fileContent)
  .filter(k => allow.indexOf(k) !== -1)
  .reduce((prev, curr) => [...prev, ...mapToContainer(fileContent[curr])], [])

console.log(containerNames)
Francis Leigh
  • 1,870
  • 10
  • 25
0

If you want to merge the three arrays and map them you can do it like the following

[...xContainers, ...yContainers, ...zContainers].map(c => <Container containerName={c} />)

if you're okay with extra libs, you can use lodash

_.map(_.concat(xContainers, yContainers, zContainers), c => (<Container containerName={c} />))
Ahmed Kamal
  • 2,660
  • 3
  • 21
  • 36