2

Well I know im doing wrong ...

var recipient=[{}];
if (content.moderators.length != 0) {
    for (var i = 0; i < content.moderators.length; i++) {
        console.log(content.moderators[i])
        recipient[i].id=content.moderators[i];
        recipient[i].status="unread";
    }
}

I’m getting an error :

TypeError: Cannot set property 'id' of undefined

What’s the correct way ...

1
  • what exactly are you trying to do? Commented Jan 25, 2017 at 15:14

5 Answers 5

1

Define a simple array [] then create object and push it to the array like :

recipient[i]= {'id':content.moderators[i], 'status': 'unread'};

Full code :

var recipient=[];
if (content.moderators.length != 0) {
    for (var i = 0; i < content.moderators.length; i++) {
        recipient[i]= {'id':content.moderators[i], 'status': 'unread'};
    }
}

Hope this helps.

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

Comments

1

This should get it:

var recipient = [];
if (content.moderators.length != 0) {
    for (var i = 0; i < content.moderators.length; i++) {
        console.log(content.moderators[i])
        recipient.push({
            id: content.moderators[i],
            status: "unread"
        });
    }
}

1 Comment

@pwolaq Probably something the OP is accustomed to doing due to nuances of other languages but I just modified the original code to work. Nit-picking about what is proper when someone is asking a direct question is more of an annoyance than it is useful.
1

Try it this way. You need to create the object at recipient[id] before assigning id to it. Here, you'll create the object with the properties populated already

var recipient = [];
if (content.moderators.length != 0) {
        for (var i = 0; i < content.moderators.length; i++) {
            recipient[i] = {
                id: content.moderators[i],
                status: "unread"
            }
        }
    }

1 Comment

I just updated his code with the correct expression without questioning the logic behind it :)
1

You could map the new object with Array#map.

The map() method creates a new array with the results of calling a provided function on every element in this array.

var content = { moderators: ['Hank', 'Jane', 'Ann', 'Bill'] },
    recipient = content.moderators.map(function(m) {
        return { id: m, status: "unread" };
    });

console.log(recipient);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6

var content = { moderators: ['Hank', 'Jane', 'Ann', 'Bill'] },
    recipient = content.moderators.map(m => ({ id: m, status: "unread" }));

console.log(recipient);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

if content.moderators is an Array

var recipient = moderators.map(id => ({ id, status: "unread" }))

Comments

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.