0

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.

Jamgreen
  • 10,329
  • 29
  • 113
  • 224
  • 2
    This seems to fly in the face of what "constant" means. – Amy Blankenship Aug 09 '16 at 20:44
  • There's at least 3 syntactical issues with your code that would not make it run at all, so I don't think you've accomplished what you think you have. – rgthree Aug 09 '16 at 21:04
  • @AmyBlankenship - While I tend to agree, Javascript `const`'s have never meant to be immutable. – Arnauld Aug 09 '16 at 21:47
  • I agree, the idea of constants in JavaScript is somewhat silly. On the topic of the original question, it's not clear of what the true goal or context is. For example, one solution that *might* be useful is to just grab a reference to the input and output objects (as they're populated?) and deal with them separately. Does it matter if these things stay the original objects? Etc. – Amy Blankenship Aug 09 '16 at 21:54

2 Answers2

1

This might be one way of doing this job;

var nodes = [
    { layer: '0' },
    { layer: 'input' },
    { layer: '0' },
    { layer: 'output' },
    { layer: '1' }
],
result = nodes.map(n => isNaN(+n.layer) ? n.layer === "input" ? {layer:1}
                                                              : {layer: n.layer}
                                        : n.layer = {layer:+n.layer + 2});
result.find(o => o.layer === "output").layer = result.filter(f => !isNaN(f.layer))
                                                     .reduce((p,c) => p.layer > c.layer ? p : c)
                                                     .layer + 1;
console.log(result);
Redu
  • 25,060
  • 6
  • 56
  • 76
1

What about this:

const nodes = [
    { layer: '0' },
    { layer: 'input' },
    { layer: '0' },
    { layer: 'output' },
    { layer: '1' }
];

nodes.filter( o => o.layer === 'output' )[0].layer = 
    1 + nodes.reduce( (max, node) => Math.max(max, 
        node.layer = +(node.layer === 'input') || +node.layer+2 || 0), 0);

console.log(nodes);

For those wondering about const here: the constant nature only protects the variable from a reassignment; but the objects stored within the array can be freely mutated.

trincot
  • 317,000
  • 35
  • 244
  • 286