Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
How do I access objects in items array in javascript

How do I access the value of the objects inside of the 'items' property

 [
  {
    _id: 61b6fb289ef81937305ea137,
    name: 'Apples',
    user: 61b5d787aa22ab26e3f8d0bb,
    items: [ [Object], [Object], [Object], [Object], [Object] ],
    __v: 5
  }
]

This is the code I wrote inside a get route that allowed me to get to where I am now. I just need to be able to get ahold of the value within the items property.

 const custom = _.capitalize(req.params.customList);
    const list = await List.find({name:custom, user:{$eq: req.user._id}});

Edit:

Thank you all for your help. After some more digging, trial and error, a nap, and some Lo-fi music I figured out the issue and how to fix it. The reason why I wanted to access the item in the array was so that I can iterate through the contents and present it on the from end. The main issue I was having was that every time I saved an item into a list it would not appear in the wrong list in both the DB and the front end.

I spent a majority of my time believing that this code was the issue

router.get('/:customList', auth, async(req, res)=>{
    const custom = _.capitalize(req.params.customList);

    const list = await List.findOne({name:custom, user:{$eq: req.user._id}});
  
    if(!list)
    {
      const newList = new List({
        name:custom,
        user:req.user._id,
      });

      newList.save();
      res.redirect('/'+custom)
    }
    else{
      res.render('home',{
        listTitle:custom, newListItems: list.items
      })
    }
});

But in actuality, it was this one

router.post("/home", auth, async(req, res)=>{

    const {newItem, list} = req.body;
   
    const item = new Item({
        name: newItem,
        user: req.user._id
        
    });
  
    if(list === req.user.username){
      
      item.save();
  
      res.redirect('/home');
    }else{
      
      // The was causing the issue
      const result = await List.findOne({name:list})

      result.items.push(item);

      result.save();

      res.redirect('/'+list);
    } 
  });

const result = await List.findOne({name:list}) would find the first list that matches and add the item into the array even though they could be multiple list names that matched.

I fixed it with this const result = await List.findOne({name:list, user:{$eq:req.user._id});. This took me three days to figure out and I'm just excited to finally figure it out. Sorry for the long post.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

// ...
const [{items}] = list; // destructure `items` property on first array element, same as: const items = list[0].items;
console.log(items); // array of objects
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!