奇数のCustomerIdを持つレコードをフェッチするためのJavaScriptのオブジェクト内のforループの反復?
以下が私たちのオブジェクトだとしましょう-
var customerDetails=
[
{
customerId:101,
customerName:"John"
},
{
customerId:102,
customerName:"David"
},
{
customerId:103,
customerName:"Mike"
},
{
customerId:104,
customerName:"Bob"
}
] 奇数のCustomerIDレコードのみを表示するには、次の条件でforループを使用します-
for(var index=0;index<customerDetails.length;index++){
if(customerDetails[index].customerId % 2 !=0){
//
}
} 例
var customerDetails=
[
{
customerId:101,
customerName:"John"
},
{
customerId:102,
customerName:"David"
},
{
customerId:103,
customerName:"Mike"
},
{
customerId:104,
customerName:"Bob"
}
]
for(var index=0;index<customerDetails.length;index++){
if(customerDetails[index].customerId % 2 !=0){
console.log("Customer Id="+customerDetails[index].customerId);
console.log("Customer
Name="+customerDetails[index].customerName);
console.log("---------------------------------------------------
--------")
}
console.log("");
} 上記のプログラムを実行するには、次のコマンドを使用する必要があります-
node fileName.js.
ここで、私のファイル名はdemo71.jsです。
出力
これにより、次の出力が生成されます-
PS C:\Users\Amit\JavaScript-code> node demo71.js Customer Id=101 Customer Name=John ----------------------------------------------------------- Customer Id=103 Customer Name=Mike -----------------------------------------------------------
-
JavaScriptのforループを使用して空のオブジェクトにプロパティを設定します。
以下は、JavaScriptのforループを使用して空のオブジェクトにプロパティを設定するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title>
-
JavaScriptでオブジェクトごとに一意のIDを作成するにはどうすればよいですか?
以下は、オブジェクトごとに一意のIDを作成するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>