0

I have an Object containing more objects that I want to iterate over. The structure is like this:

Objects in Objects

Eventually, I want to create an array where the key of the parent object points to the value in the child object's title column, so I can post later to some PHP script, i.e, I want:

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

But I am really struggling to actually loop through the array.

I have tried the following two methods, and while I can loop over the parent object, I seem to be struggling to get access to the values in the child, they just show as the index position, or undefined respectively.

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

Any help would be appreciated, as I am really scratching my head, and have tried a few things online.

5
  • in order to help you,Please provide the object Commented May 23, 2019 at 15:24
  • I tried to in the picture I included. Is that not enough? Commented May 23, 2019 at 15:28
  • It will be enough when you'll be able to copy/paste text from/to a picture. Commented May 23, 2019 at 15:32
  • What do you want to do with those values? Because columns['title']; only accesses the value in the property, it doesn't do anything with it. Commented May 23, 2019 at 15:35
  • Sorry that's actually I typo on my part, I was using console.log on it Commented May 23, 2019 at 15:50

1 Answer 1

1

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.

You can't generate an array with those properties in Javascript like you do in PHP: An array in PHP is actually an ordered map. A map is a type that associates values to keys. But you can create a Javascript Object, or a Map. However, I believe the first options is the one you need if you are going to send data to some server-side PHP script.

I believe you are searching for this:

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;}

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect that was just what I was looking for. I wasn't expecting it to be that easy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.