0

I have one array of publisher objects and one array of idea objects that were created by those publishers. Each idea object contains a 'user:' key with the publisher's Id. I need to take each of the idea objects and add the publisher's username, so that it will have both the publisher's Id and username. In other words, I'm taking only certain elements from each object and creating a new array of objects.

Example:

const publisherObjects = [
  {
    username: 'Curtis',
    description: 'description',
    email: 'email@email.com'
    id: '111'
  }, {
  ...
  }
]

and


const ideaObjects = [
  {
    title: 'new idea',
    thesis: 'this is my idea',
    user: '111'
  }, {
  ...
  }
]

into


const ideasWithUsernames = [
  {
    title: 'new idea',
    thesis: 'this is my idea',
    user: '111',
    username: 'Curtis'
  }, {
  ...
  }
]

I have tried this:

let ideas = ideaObjects.map(i => {
  let publisher = publisherObjects.find(p => p._id === i.user)
  if (publisher)
     return {
        id: i._id,
        title: i.title,
        thesis: i.thesis,
        ticker: i.ticker,
        longOrShort: i.longOrShort,
        publisher: {
          id: i.user,
          username: p.username
        }
     }
} 

I expected this to return an array of objects like above, however instead I get an array of 'undefined'.

[
  undefined, undefined,
  undefined, undefined,
  undefined, undefined,
  undefined, undefined
]

Any help would be greatly appreciated.

curtis
  • 17
  • 4
  • Are you sure that you can compare `new ObjectId("111")` and `111` with `===` it doesn't seem right. What is `ObjectId`? – Konrad Jan 24 '23 at 21:16
  • Yes sorry they both use new ObjectId, however I just now removed it so as to avoid confusion. ObjectId is what mongoose calls an Id that it generates. – curtis Jan 24 '23 at 21:22
  • The output format in your code does not match your expected output. What is the real expected output? – Fractalism Jan 24 '23 at 21:30
  • My expected output is what is inside the return statement. I expect an array of objects with that format. – curtis Jan 24 '23 at 21:33

1 Answers1

0

This seems to work. Is this what you want?

const publisherObjects = [
    {
        username: 'Curtis',
        description: 'description',
        email: 'email@email.com',
        id: '111',
    },
]

const ideaObjects = [
    {
        id: '123',
        title: 'new idea',
        thesis: 'this is my idea',
        ticker: 'foo',
        longOrShort: 'bar',
        user: '111',
    },
]

let ideas = ideaObjects.map(ideaObj => {
    let publisher = publisherObjects.find(p => p.id === ideaObj.user)
    if (!publisher) return null;
    let newIdeaObj = {
        ...ideaObj,
        publisher: {
            id: ideaObj.user,
            username: publisher.username,
        }
    }
    delete newIdeaObj.user;
    return newIdeaObj;
})

console.log(ideas);
Fractalism
  • 1,231
  • 3
  • 12
  • This is what I'm looking for, however it is now returning an array of null values. I have console logged the p.id and ideaObj.user and confirmed that they are often the exact same Ids, however it still does not pass the strict equality comparison nor a loose equality comparison. Weird – curtis Jan 24 '23 at 22:04
  • Have you checked that the data you posted matches your own local data? Cuz if you run the code snippet in this answer, the result is not an array of nulls. – Fractalism Jan 24 '23 at 22:08
  • 1
    Okay I got it now. The Ids I'm using are mongoose Ids, which have to be converted to strings in order to compare. Your solution now works as long as I add String() before referencing each Id in the equality comparison. Thanks – curtis Jan 24 '23 at 22:33
  • https://stackoverflow.com/a/11638106 – Konrad Jan 25 '23 at 00:19