配列およびオブジェクトの配列内の一般的なアイテムにプロパティを追加します-JavaScript?
プロパティを追加するには、map()を使用します。以下が私たちの配列だとしましょう-
const firstname = ['John', 'David', 'Bob'];
以下はオブジェクトの配列です-
const studentDetails = [
{
firstname: 'Carol',
marks: 78
},
{
firstname: 'Mike',
marks: 89
},
{
firstname: 'Bob',
marks: 86
}
]; 例
以下はコードです-
const firstname = ['John', 'David', 'Bob'];
const studentDetails = [
{
firstname: 'Carol',
marks: 78
},
{
firstname: 'Mike',
marks: 89
},
{
firstname: 'Bob',
marks: 86
}
];
const data = new Set(firstname);
const result = studentDetails.map(tmpObject => {
if (data.has(tmpObject.firstname)) tmpObject.isPresent ="This is present";
else
tmpObject.isPresent = "This is not present";
return tmpObject;
});
console.log(result); 上記のプログラムを実行するには、次のコマンドを使用する必要があります-
node fileName.js.
ここで、私のファイル名はdemo219.jsです。
出力
出力は次のとおりです-
PS C:\Users\Amit\JavaScript-code> node demo219.js
[
{ firstname: 'Carol', marks: 78, isPresent: 'This is not present' },
{ firstname: 'Mike', marks: 89, isPresent: 'This is not present' },
{ firstname: 'Bob', marks: 86, isPresent: 'This is present' }
] -
JavaScript配列長プロパティ
Array lengthプロパティは、指定された配列に存在する要素の総数である配列の長さを設定または返します。 以下は、配列の長さプロパティ-のコードです。 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title&
-
JavaScript-配列オブジェクトの長さ
JavaScriptのlengthプロパティは、オブジェクトのサイズを返します。以下は、文字列および配列オブジェクトの長さのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document