Javascript 对象值变量
Javascript object value variable
我想在 js 对象中进行一些计算。这可能吗?
foo: [
{
value: 1000
target:50
process: (target*value)/100
},
{
value: 500
target:100
process: (target*value)/100
}]
process
密钥应根据 value
和 target
计算得出。有办法吗?js
你不能这样做。
为什么不
const process = (target*value)/100;
foo: [
{
value: 1000
target:50
process: process
},
{
value: 500
target:100
process: process
}]
或者,如果您愿意,Class 可能对这种情况有用:
class Foo
{
constructor: (value, target) {
this.value = value;
this.target = target;
this.process = (target*value)/100;
}
}
然后只是:
foo: [
new Foo(500, 100),
new Foo(1000, 50)]
您可以将 process
设为 getter:
const foo = [
{
value: 1000,
target: 50,
get process() {
return (this.target * this.value) / 100;
}
},
{
value: 500,
target: 100,
get process() {
return (this.target * this.value) / 100;
}
}
];
然后使用属性访问:
console.log(foo[0].process); //=> 500
console.log(foo[1].process); //=> 500
您可以先将“原始值”(value
和 target
属性)存储在数组中:
const rawData = [{ value: 1000, target: 50 }, { value: 500, target: 100 }];
然后 map
添加计算的 属性:
const foo = rawData.map(({ value, target }) => ({ value, target, process: target * value / 100 }));
进程应该是函数或访问器:
var foo= [
{
value: 1000,
target:50,
process() { return (this.target*this.value)/100}
},
{
value: 500,
target:100,
process() { return (this.target*this.value)/100}
}]
// use it like this:
console.log(foo[0].process()); //=> 500
console.log(foo[1].process()); //=> 500
假设对象在javascript,在对象后添加一些计算。像这样:
let foo = [{
value: 1000,
target:50,
process:0
},
{
value: 500,
target:100,
process:0
}];
foo[0]["process"] = foo[0]["target"] * foo[0]["value"];
foo[1]["process"] = foo[1]["target"] * foo[1]["value"];
console.log(foo[0]["process"]); // ==> 50000
console.log(foo[1]["process"]); // ==> 50000
我想在 js 对象中进行一些计算。这可能吗?
foo: [
{
value: 1000
target:50
process: (target*value)/100
},
{
value: 500
target:100
process: (target*value)/100
}]
process
密钥应根据 value
和 target
计算得出。有办法吗?js
你不能这样做。 为什么不
const process = (target*value)/100;
foo: [
{
value: 1000
target:50
process: process
},
{
value: 500
target:100
process: process
}]
或者,如果您愿意,Class 可能对这种情况有用:
class Foo
{
constructor: (value, target) {
this.value = value;
this.target = target;
this.process = (target*value)/100;
}
}
然后只是:
foo: [
new Foo(500, 100),
new Foo(1000, 50)]
您可以将 process
设为 getter:
const foo = [
{
value: 1000,
target: 50,
get process() {
return (this.target * this.value) / 100;
}
},
{
value: 500,
target: 100,
get process() {
return (this.target * this.value) / 100;
}
}
];
然后使用属性访问:
console.log(foo[0].process); //=> 500
console.log(foo[1].process); //=> 500
您可以先将“原始值”(value
和 target
属性)存储在数组中:
const rawData = [{ value: 1000, target: 50 }, { value: 500, target: 100 }];
然后 map
添加计算的 属性:
const foo = rawData.map(({ value, target }) => ({ value, target, process: target * value / 100 }));
进程应该是函数或访问器:
var foo= [
{
value: 1000,
target:50,
process() { return (this.target*this.value)/100}
},
{
value: 500,
target:100,
process() { return (this.target*this.value)/100}
}]
// use it like this:
console.log(foo[0].process()); //=> 500
console.log(foo[1].process()); //=> 500
假设对象在javascript,在对象后添加一些计算。像这样:
let foo = [{
value: 1000,
target:50,
process:0
},
{
value: 500,
target:100,
process:0
}];
foo[0]["process"] = foo[0]["target"] * foo[0]["value"];
foo[1]["process"] = foo[1]["target"] * foo[1]["value"];
console.log(foo[0]["process"]); // ==> 50000
console.log(foo[1]["process"]); // ==> 50000