迭代 Object 共 Object 秒以创建数组

Iterating over Object of Objects to create Array

我有一个 Object,其中包含我想要迭代的更多 objects。结构是这样的:

最后,我想创建一个 array,其中 parent objectkey 指向 valuechild object 的标题列 中,所以我可以 post 稍后到一些 PHP 脚本,即, 我要:

BusinessJustification => 'titleValue'
Contract => 'titleValue'
...

但我真的很难实际遍历数组。

我尝试了以下两种方法,虽然我可以遍历 parent object,但我似乎很难访问 child 中的值,它们仅显示为索引位置,或分别显示为 undefined

for (var fields in this.postData) {
    for (var columns in fields){
        console.log(columns['title']);
    }
}

for (var fields in this.postData) {
    for (var columns in fields){
        console.log(Object.entries(columns['title']));
    }
}

任何帮助将不胜感激,因为我真的很挠头,并且已经在线尝试了一些东西。

I eventually want to create an array where the key of the parent object points to the value in the child object's title column.

您不能像在 PHP 中那样使用 Javascript 中的这些属性生成 arrayPHP 中的数组实际上是有序 mapmap 是一种将 values 关联到 keys 的类型。但是你可以创建一个JavascriptObject, or a Map。但是,如果您要将数据发送到某些 server-side PHP 脚本,我相信第一个选项是您需要的选项。

我相信你正在搜索这个:

const postData = {
    BusinessJustification: {blur: true, title: "business-title", valid: true},
    Contact: {blur: true, title: "contact-title", valid: true},
    Department: {blur: true, title: "department-title", valid: true},
}

let newObj = {};

for (const field in postData)
{
  newObj[field] = postData[field].title;
}

console.log(newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}