javascript - How to spread object inside object - Stack Overflow

I Have an object inside another object and I want to spread the inner one, my reason is when I want to

I Have an object inside another object and I want to spread the inner one, my reason is when I want to call the object by it's id

My object

Resolver [
  { _id: { _id: '123456789', totaloute: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_APPROVED_ONLINE' },
    count: 33
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_DECLINED' },
    count: 15
  }
]

As shown above I want to append 'count' attribut inside '_id' attribute , i couldn't spread the inner one so How can I do it

I Have an object inside another object and I want to spread the inner one, my reason is when I want to call the object by it's id

My object

Resolver [
  { _id: { _id: '123456789', totaloute: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_APPROVED_ONLINE' },
    count: 33
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_DECLINED' },
    count: 15
  }
]

As shown above I want to append 'count' attribut inside '_id' attribute , i couldn't spread the inner one so How can I do it

Share Improve this question asked May 18, 2022 at 10:35 NezihNezih 191 silver badge3 bronze badges 3
  • array.map(({ _id, count }) => ({ ..._id, count })) like this? – adiga Commented May 18, 2022 at 10:43
  • Or array.map(({ _id, count }) => Object.assign(_id, { count })) depending upon where you want the count property. If possible, fix this in the server side where you are grouping and counting. – adiga Commented May 18, 2022 at 10:44
  • I can't fix this in the server cuz it's the syntax of mongoDB , it's grouping method – Nezih Commented May 18, 2022 at 10:49
Add a ment  | 

3 Answers 3

Reset to default 3

You can do this using the spread operator:

array.map((item) => {
  return { ...item, ...item._id };
});

How to spread object inside object

We are useing ... for spreding object into inner obj.

const user = {
  name:"siva",
  age: 25
}

//spred user in to below obj
const userInfo = {
   _id:"123445",
   phno: 77777777777
   ...user
}```

Code

const Resolver= [
  { _id: { _id: '123456789', totaloute: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_APPROVED_ONLINE' },
    count: 33
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloute: 'OUTCOME_DECLINED' },
    count: 15
  }
]

const result = Resolver.map(({_id, ...rest}) => {
  return { ..._id, ...rest };
});

console.log(result)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745190714a4615850.html

相关推荐

  • javascript - How to spread object inside object - Stack Overflow

    I Have an object inside another object and I want to spread the inner one, my reason is when I want to

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信