Home > Mobile >  How to add aditional object property in array of objects?
How to add aditional object property in array of objects?

Time:08-09

I'm trying to dynamically add additional property in the existing object. This is what I have:

let items = [
   {name: 'Carlsberg', category: 'Beer', price: '4.99'},
   {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
   {name: 'Miller Lite', category: 'Beer', price: '7.99'}
]

These are the object properties which I wish to add:

categories:{
     beerCounter:2,
     vodkaCounter:1,
}

And what I want is to dynamically add additional properties of an object to look like this:

items = [
       {name: 'Carlsberg', category: 'Beer', price: '4.99', counter: categories.beerCounter},
       {name: 'Smirnoff', category: 'Vodka', price: '28.99', counter: categories.vodkaCounter},
       {name: 'Miller Lite', category: 'Beer', price: '7.99', counter: categories.beerCounter}
    ]

CodePudding user response:

Using Array.forEach:

let items = [
   {name: 'Carlsberg', category: 'Beer', price: '4.99'},
   {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
   {name: 'Miller Lite', category: 'Beer', price: '7.99'}
];

const categories = {
     beerCounter: 2,
     vodkaCounter: 1,
};

items.forEach(item => {
  item.counter = item.category === 'Beer' ? categories.beerCounter : categories.vodkaCounter;
})

console.table(items);

CodePudding user response:

You could just iterate your array using forEach, setting the counter property using the lower-cased category value for each item:

let items = [{
    name: 'Carlsberg', category: 'Beer', price: '4.99'
  },
  {
    name: 'Smirnoff', category: 'Vodka', price: '28.99'
  },
  {
    name: 'Miller Lite', category: 'Beer', price: '7.99'
  }
]

let categories = {
  beerCounter: 2,
  vodkaCounter: 1,
}

items.forEach(i => i.counter = categories[`${i.category.toLowerCase()}Counter`])

console.log(items)

CodePudding user response:

You can do this with the Array.forEach() method.

I advise you to do this with a regex, it avoids keyboard errors if your category ever comes in all lowercase or all uppercase, or even a mixture of the two, sometimes our fingers wander from typing code haha

let items = [
    {name: 'Carlsberg', category: 'Beer', price: '4.99'},
    {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
    {name: 'Miller Lite', category: 'Beer', price: '7.99'}
 ]

const categories = {
    beerCounter: 2,
    vodkaCounter: 1,
};

items.forEach(item => {
    if (/^beer/i.test(item.category)) {
        item.counter = categories.beerCounter;
    }
    if (/^vodka/i.test(item?.category)) {
        item.counter = categories.vodkaCounter;
    }
});

console.log(items);

Good luck dude !

CodePudding user response:

let items = [
   {name: 'Carlsberg', category: 'Beer', price: '4.99'},
   {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
   {name: 'Miller Lite', category: 'Beer', price: '7.99'}
]

items.forEach((item) => {
  if(item.category === 'Beer'){
  item.categories = categories.beerCounter
}
})

CodePudding user response:

You can map it like this. Basically the idea is to loop through all of the items and then use each item's category to map to a key in the categories object to grab the counter value for each item.

let items = [
   {name: 'Carlsberg', category: 'Beer', price: '4.99'},
   {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
   {name: 'Miller Lite', category: 'Beer', price: '7.99'}
]
let categories = {
   beerCounter:2,
   vodkaCounter:1,
}
for (let item of items) {
  item.couter = categories[`${item.category.toLowerCase()}Counter`]
}

console.log(items);

CodePudding user response:

You can use map or forEach

let items = [
   {name: 'Carlsberg', category: 'Beer', price: '4.99'},
   {name: 'Smirnoff', category: 'Vodka', price: '28.99'},
   {name: 'Miller Lite', category: 'Beer', price: '7.99'}
]

const categories = {
    beerCounter:2,
    vodkaCounter:1,
}

items.map(item=>{
  if(item.category === 'Beer')
    item.counter = categories.beerCounter;
  if(item.category === 'Vodka')
    item.counter = categories.vodkaCounter;
})

console.log(items);
  • Related