मान लीजिए निम्नलिखित हमारा ऑब्जेक्ट है:
var employee =
[
{ name: "John", amount: 800 },
{ name: "David", amount: 500 },
{ name: "Bob", amount: 450 }
]
हमें "amount" वैल्यू को 2 से गुणा करना है, केवल तभी जब amount 500 से अधिक हो यानी अपेक्षित आउटपुट होना चाहिए:
[
{ name: 'John', amount: 1600 },
{ name: 'David', amount: 500 },
{ name: 'Bob', amount: 900 }
]
उदाहरण
ऑब्जेक्ट वैल्यू को गुणा करने के लिए नमूना उदाहरण यहां है:
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)
उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्नलिखित कमांड का उपयोग करना होगा:
node fileName.js
यहां, मेरा फ़ाइल नाम demo257.js है।
आउटपुट
यह कंसोल पर निम्नलिखित आउटपुट देगा:
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 }
]