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' } ]