1

I am trying to add the new property to the array of objects but while iterating, I am losing the existing values.

Is there any way, I can modify just the object's property and also do a null check?

I tried following.

const data = [
  {
    testid: 1,
    items: {
      id: 1,
      desc: 'test'
    }
  },
  {
    testid: 2,
    items: {
      id: 2,
      desc: 'test1'
    }
  },
  {
    testid: 3
  }
];

let result = data.map(({ items }) => ({
  ...items,
  newval: items?.id + ' - ' + items?.desc
}));
console.log(result);

//Expected Output

const newData = [
  {
    testid: 1,
    items: {
      id: 1,
      desc: 'test',
      newval: '1 -test'
    }
  },
  {
    testid: 2,
    items: {
      id: 2,
      desc: 'test1',
      newval : '2 -test1'
    }
  },
  {
    testid: 3
  }
];

3 Answers 3

3

You need to clone the parent object of items as well, and then conditional clone the items sub object if one already exists:

const data = [{"testid":1,"items":{"id":1,"desc":"test"}},{"testid":2,"items":{"id":2,"desc":"test1"}},{"testid":3}];

const result = data.map(({ items, ...rest }) => ({
  ...rest, // clone the object
  ...items && { // clone the items and add the property if items exist
    items: {
      ...items,
      newval: items?.id + ' - ' + items?.desc
    }
  }
}));

console.log(result);

If you have an array of items instead of a single object, use another map:

const data = [ { testid: 1, items: [ { id: 1, desc: 'test' }, { id: 11, desc: 'test11' } ] }, { testid: 2, items: [ { id: 2, desc: 'test1' }, { id: 21, desc: 'test111' } ] }, { testid: 3 } ];

const result = data.map(({ items, ...rest }) => ({
  ...rest, // clone the object
  ...items && {
    items: items.map(item => ({
      ...item,
      newval: item?.id + ' - ' + item?.desc
    }))
  }
}));

console.log(result);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Ori, Just have one doubt. If I have items as an array of objects instead of one object, how I can iterate that? do I need to add one more map function?
const data = [ { testid: 1, items: [ { id: 1, desc: 'test' }, { id: 11, desc: 'test11' } ] }, { testid: 2, items: [ { id: 2, desc: 'test1' }, { id: 21, desc: 'test111' } ] }, { testid: 3 } ];
You'll need to map the items, instead of just spreading them. See updated answer.
1

You need to expand items property

const data = [{
    testid: 1,
    items: {
      id: 1,
      desc: 'test'
    }
  },
  {
    testid: 2,
    items: {
      id: 2,
      desc: 'test1'
    }
  },
  {
    testid: 3
  }
];

let result = data.map((items) => ({
  ...items,
  items: {
    ...items.items,
    newval: items.items?.id + ' - ' + items.items ?.desc
  }
}));

console.log(result);

Comments

0

I think you're trying to simplify the code too much. Here is a more verbose version that I think is easier to read:

const data=[{testid:1,items:{id:1,desc:"test"}},{testid:2,items:{id:2,desc:"test1"}},{testid:3}];

let result = data.map(el => 
{
  if(el.items)
  {
    el.items.newval = `${el.items.id} - ${el.items.desc}`
    
    return {
      testid: el.testid,
      items: el.items
    }
  }
  else
  {
    return el
  }
})
console.log(result)

Comments

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.