2

How to assign incremental index to nested array of object below? each members will have a property 1,2,3,4

groupMembersByTitle = [{
    members: [{
        id: 'uuid'
    }, {
        id: 'uuid'
    }]
}, {
    members: [{
        id: 'uuid'
    }, {
        id: 'uuid'
    }]
}]

I'm stuck here

const r = groupMembersByTitle.map(o => ({
    ...o,
    members: o.members.map((o2, index) => ({
        ...o2,
        no: ++index
    }))
}))

4 Answers 4

3

You'll need a more persistent outer variable.

const groupMembersByTitle = [{
    members: [{
        id: 'uuid'
    }, {
        id: 'uuid'
    }]
}, {
    members: [{
        id: 'uuid'
    }, {
        id: 'uuid'
    }]
}];

let no = 1;
const mapped = groupMembersByTitle.map(
  obj => ({
    members: obj.members.map(
      member => ({ ...member, no: no++ })
    )
  })
);
console.log(mapped);

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

Comments

1

You can use the map parameter thisArg.

map(function(element, index, array) { /* … */ }, thisArg)

const groupMembersByTitle = [{
  members: [{ id: 'uuid' }, { id: 'uuid' }]
}, {
  members: [{ id: 'uuid' }, { id: 'uuid' }]
}];

const r = groupMembersByTitle.map(function(o1) {
  return ({
    members: o1.members.map(
      o2 => ({
        ...o2,
        no: ++this.acc
      })
    )
  });
}, { acc: 0 });

console.log(r);

1 Comment

this is new to me, worth an upvote although it's not explicit
0

function recursiveFunction(data, index) {
    if(Array.isArray(data)) {
      return data.reduce((acc, el) => this.recursiveFunction(el, acc), index);
    } else if(data.members) {
      return this.recursiveFunction(data.members, index);
    } else {
      data.no = index;
      return index + 1;
    }
  }

const groupMembersByTitle = [
  {
    members: [
      {
        id: 'uuid'
      },
      {
        id: 'uuid'
      }
    ]
  },
  {
    members: [
      {
        id: 'uuid'
      },
      {
        id: 'uuid'
      }
    ]
  }];


const res = recursiveFunction(groupMembersByTitle, 0);
console.log(groupMembersByTitle);
console.log(res);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can do it using Array.prototype.map twice and use the indices of the arrays to calculate the no.

const 
  data = [
    { members: [{ id: "uuid" }, { id: "uuid" }] },
    { members: [{ id: "uuid" }, { id: "uuid" }] },
  ],
  result = data.map(({ members }, i) =>
    members.map((m, j) => ({
      ...m,
      no: i * (i ? data[i - 1].members.length : 1) + j + 1,
    }))
  );

console.log(result);

2 Comments

your output print: 1,2,2,3
@Florence I've updated my answer, it now works as expected.

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.