1

I want to change one item from array from app locals variable and i am not sure how to do this

Here is what i have set as global

app.locals.products=[{name: 'a',url: '/a' },
    {name: 'b',url: '/b' },
    {name: 'c',url: '/c' },...

I want to edit them and set active product from routes and do something like this

    products:[{name: 'a',url: '/a' active:true}],

But when i do this it will remove all the other items and set only the product the one i wrote. Is there a way to edit just the one i need and leave all the rest unchanged?

2
  • 1
    you need to find index of product ... products[index].active = true Commented Mar 15, 2017 at 8:38
  • @David can you give me an example? Commented Mar 15, 2017 at 14:27

1 Answer 1

2

You can use the array find function to find a specific item in the products array and edit it.

function setActive(name) {

  var element = products.find(function(product) {
    return product.name === name;
  });

  if (element) {
    element.active = true;
  }
}

This function takes the name variable, checks if it exists in the product array and sets it to active. You might want to throw an exception if it doesn't.

Usage:

setActive('a');
Sign up to request clarification or add additional context in comments.

10 Comments

i get error 500... i used it in routes inside router get.. Could this be because i have spaces?
here is the global array app.locals.products=[{name: 'Window & residential door systems',url: '/en/products/window-residential-door-systems' }, {name: 'Sliding systems',url: '/en/product/sliding_system' }, {name: 'Shutter systems',url: '/en/product/shutter_system' }, {name: 'Sheets',url: '/en/product/sheets' }, {name: 'Building profiles & systems',url: '/en/product/profiles&systems' }];
and this is the routesvar express = require('express'); var router = express.Router(); router.get('/', function(req, res, next) { var url = req.originalUrl; function setActive(name) { var element = products.find(function(product) { return product.name === name; }); if (element) { element.active = true; }} setActive('Window & residential door system'); res.render('index', { title: 'The premium brand', pageurl: url, welcome: true }); }); module.exports = router;
Did you put the function definition inside the router.get function? it should be outside of it, of course, and only the setActive call should be inside .. the spaces should be no problem
Ok i have found what was the problem, i had to request the locals variable first and also place all the code including the setActive outside from the render... function setActive(name) {var element = req.app.locals.products.find(function(product) {return product.name === name;}); if (element) {element.active = true;}} setActive('a'); thanks for the help!
|

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.