0

There is a function which create the same objects (with different values of ID, task and point) and push it to array Box. And i want to get each number of object, and show it to user on page. 1,2,3,4,5,6.... How to do it?

box: [{
id: shortid.generate(),
task: action.task,
point: false
}]

9
  • 3
    Can you provide the code you're talking about? Commented Apr 23, 2020 at 17:54
  • Added. But there's a fucntion which create the same objects, but with own properties, and I want to show to user number of object. Commented Apr 23, 2020 at 17:57
  • Can you provide where do you use arr.length? Commented Apr 23, 2020 at 17:57
  • 1
    @EugenSunic, I just want to show object's number on the web page. For example in To-Do-List. The first = 1 number, the second task = 2 number Commented Apr 23, 2020 at 18:03
  • 2
    All you want is to get the index of object in an array. And like Eugen Sunic comment, array.map((item, index)=>{}) has index parameter. check:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 23, 2020 at 18:03

1 Answer 1

1

I believe you want to add sequence number to each of your object:

const result = [{
  id: '2s3wf4',
  task: 'task1',
  point: false
}].map((x, i) => ({
  ...x,
  serialNumber: i + 1
}));


console.log(result)

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

3 Comments

There is a function which create the same objects (with different values of ID, task and point) and push it to array Box. And i want to get each number of object, and show it to user on page. 1,2,3,4,5,6....
@БогданЧубко please update you description then of what exactly you want it's a little bit unclear...
I told you above what I need.

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.