【发布时间】:2022-01-08 14:45:45
【问题描述】:
我正在尝试使用占位符创建一个对象。当前代码有效,但是它正在我的 Children 属性中创建数组。如何重新编码以便我的输出是对象
const placeholder = (index, isRootDirectory) => {
const create = {
id: index,
title: null,
location: index,
isRootDirectory,
padStyle: [
{
icon: null,
color: null,
},
],
UUID: null,
action: null,
Children: [],
};
return create;
};
const createLayout = () => {
let root = {
info: "Custom Layout",
Children: [],
};
for (let i = 0; i < 2; i++) {
root.Children.push(placeholder(i, true));
console.log(root);
}
console.log(root);
};
当前输出:
{"Children": [{"Children": [Array], "UUID": null, "action": null, "id": 0, "isRootDirectory": true, "location": 0, "padStyle": [Array], "title": null}, {"Children": [Array], "UUID": null, "action": null, "id": 1, "isRootDirectory": true, "location": 1, "padStyle": [Array], "title": null}], "info": "Custom Layout"}
期望的输出
{"Children": [{"Children": [object], "UUID": null, "action": null, "id": 0, "isRootDirectory": true, "location": 0, "padStyle": [object], "title": null}, {"Children": [object], "UUID": null, "action": null, "id": 1, "isRootDirectory": true, "location": 1, "padStyle": [object], "title": null}], "info": "Custom Layout"}
【问题讨论】:
-
这是在 Node.js 中吗?您似乎误解了输出。见How can I get the full object in Node.js's console.log(), rather than '[Object]'?。
-
React Native 但我觉得 Node 中的任何代码都是一样的
标签: javascript arrays object