I have an array
const nodes = [
{ layer: '0' },
{ layer: 'input' },
{ layer: '0' },
{ layer: 'output' },
{ layer: '1' }
};
I want to keep the array, but all the layer values should be changed.
The input layer should have value 1, all the numeric values should be increased by 2 and the output layer should have the new highest numeric value plus 1. All the values should be numbers instead of strings.
So the new array will be
const nodes = [
{ layer: 2 },
{ layer: 1 },
{ layer: 2 },
{ layer: 4 },
{ layer: 3 }
};
I have accomplished this with
const nodes = [
{ layer: '0' },
{ layer: 'input' },
{ layer: '0' },
{ layer: 'output' },
{ layer: '1' }
};
const output = Math.max.apply(Math, nodes.map((node) => Number.parseInt(node.layer, 10) || 0)) + 3;
nodes.map((node) => {
layer: neuron.layer === 'input' ? 1 : (neuron.layer === 'output' ? output : Number.parseInt(neuron.layer, 10) + 2)
})
It seems to work, but the code is really ugly.
I wonder if it can be done more neat than this.