JavaScript向JSON对象添加数组属性[ES5]并在控制台中显示对象属性?
以下是我们的对象 −
var customerDetails ={ "customerFirstName":"David", "customerLastName":"Miller", "customerAge":21, "customerCountryName":"US" };
现在,创建一个新的数组并使用push()函数。以下为相应的代码 −
示例
var customerDetails ={ "customerFirstName":"David", "customerLastName":"Miller", "customerAge":21, "customerCountryName":"US" }; var customerObjectToArray = []; customerObjectToArray.push(customerDetails); console.log(customerObjectToArray);
要运行以上程序,您需要使用以下命令 −
node fileName.js.
此处,我的文件名是 demo125.js。
输出
这会生成以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo125.js [ { customerFirstName: 'David', customerLastName: 'Miller', customerAge: 21, customerCountryName: 'US' } ]
广告