I have an array of javascript objects, and I want to create a key:value pair that is the product of two values.
I can solve this using much longer code outside of the object (i.e. in a 'for each' loop), but I thought there must be an easier way. Perhaps you could point me to the right direction?
let DB = [
{
number: 100,
factor: 1.5
product: this.number * this.factor // doesn't work :(
},
{
...
}
];
I want product to equal 150, which is 100 * 1.5, but I don't know how to get access to those 2 values.
let DB = [ { number: 100, factor: 1.5 }, { ... } ].map( o => ({ ...o, product: o.number * o.factor }) );numberandfactorand you want to add a new one callledproductas described (= number * factor)?.map(), but I suggest exploring the other array methods in the left navigation bar - there's lots of useful stuff there.