Giả sử sau đây là đối tượng của chúng tôi -
var employee =
[
{ name: "John", amount: 800 },
{ name: "David", amount: 500 },
{ name: "Bob", amount: 450 }
] Chúng ta cần nhân giá trị "số tiền" với 2, chỉ khi số tiền lớn hơn 500, tức là sản lượng mong đợi phải là -
[
{ name: 'John', amount: 1600 },
{ name: 'David', amount: 500 },
{ name: 'Bob', amount: 900 }
] Ví dụ
Đây là ví dụ mẫu để nhân các giá trị đối tượng -
var employee =
[
{ name: "John", amount: 800 },
{ name: "David", amount: 500 },
{ name: "Bob", amount: 450 }
]
console.log("Before multiplying the result=")
console.log(employee)
for (var index = 0; index < employee.length; index++) {
if (employee[index].amount > 500) {
employee[index].amount = employee[index].amount * 2;
}
}
console.log("After multiplying the result=")
console.log(employee) Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo257.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau trên bảng điều khiển -
PS C:\Users\Amit\javascript-code> node demo257.js
Before multiplying the result=
[
{ name: 'John', amount: 800 },
{ name: 'David', amount: 500 },
{ name: 'Bob', amount: 450 }
]
After multiplying the result=
[
{ name: 'John', amount: 1600 },
{ name: 'David', amount: 500 },
{ name: 'Bob', amount: 900 }
]