Home > Net >  How to conditionally change values of keys of an object in an object array in Typescript
How to conditionally change values of keys of an object in an object array in Typescript

Time:07-03

I have the following object and it's keys in the array named objectArray of clothesTypes[].

const objectArray: clothesTypes[] = [
  {
    trousers: 90,
    'skirts and jackets': 47,
    scarfs: 100,
  },
]

Then I have another array of type userData[], which is being updated (when a user buys cloth).

const userArray: userData[] = [
  {
    name: 'David',
    clothType: 'trousers',
    price: 45,
  },
  {
    name: 'Joanna',
    clothType: 'scarfs',
    price: 5,
  },
  {
    name: 'Gillian',
    clothType: 'trousers',
    price: 35,
  },
]

What I need to do is update the values of keys of first objectArray, by taking the number of objects with the relevant 'clothType' property in the userArray. In this example, the trousers should be assigned 2, 'skirts and jackets' should be assigend 0, and scarfs should be assigned 1.


Following is the method I tried:

objectArray.map(item => ({
  ...item,
  ...{
    trousers: userArray.filter(d => d.clothType === 'trousers').length,
    'skirts and jackets': userArray.filter(d => d.clothType === 'skirts and jackets').length,
    scarfs: userArray.filter(d => d.clothType === 'scarfs').length,
  },
})),

How can I do the above at once, without assigning the values to each key individually?

thank you!

CodePudding user response:

Author of the question indicated that objectArray is an array with length always 1. With that in mind, here is my suggestion -

Like I said in a comment above, there is no way to do it all at "once", you will have to iterate and count but you can improve the algorithm slightly (Especially, if the objectArray doesn't need to be an array because the length is always one)

Link to full working example in TypeScript Playground

const userArray: userData[] = [
  {
    name: 'David',
    clothType: 'trousers',
    price: 45,
  },
  {
    name: 'Joanna',
    clothType: 'scarfs',
    price: 5,
  },
  {
    name: 'Gillian',
    clothType: 'trousers',
    price: 35,
  },
]

const clothesCount: clothesTypes = {
    trousers: 90,
    'skirts and jackets': 47,
    scarfs: 100,
}

userArray.reduce((pv, cv) => {
  switch(cv.clothType) {
    case 'scarfs':
      pv['scarfs']  = 1;
      break;
    case 'skirts and jackets':
      pv['skirts and jackets']  = 1;
      break;
    case 'trousers': 
      pv['trousers']  = 1;
      break;
  }
  return pv;
}, clothesCount)

CodePudding user response:

You can look over userArray and add the count if that property exist in objectArray or not.

const objectArray: clothesTypes[] = [{
  trousers: 90,
  'skirts and jackets': 47,
  scarfs: 100,
}, ];

const userArray: userData[] = [{
    name: 'David',
    clothType: 'trousers',
    price: 45,
  },
  {
    name: 'Joanna',
    clothType: 'scarfs',
    price: 5,
  },
  {
    name: 'Gillian',
    clothType: 'trousers',
    price: 35,
  },
];

const objectArrayV = objectArray[0];
userArray.forEach((obj) => {
  const { clothType } = obj;
  if (clothType in objectArrayV) {
    objectArrayV[clothType]  ;
  }
});

console.log(objectArray);

  • Related