I am using react application in iteration I have a map method with some doubt, I have a select box with fill number based on iteration 0 to coming value base,my expected output is [{label:0,value:0},{label:1,value:1},{label:2,value:2}] but coming output is =[{label:1,value:1},{label:2,value:2}], I know how to do in for loop but, i want know map method beacuse i am react application,i tried fill , and join method it's not working please help uss
let value = 2
[...Array(parseInt(value)).keys()].map((x, index) => (
{
label: x 1,
value: x 1,
}
))
CodePudding user response:
Array(2);
will create an empty array with length is 2, so when you want your array is [0, 1, 2], the length should be Array(3);
. So I update your code into Array(value 1)
and remove parseInt
.
let value = 2
const result = [...Array(value 1).keys()].map((x) => ({
label: x,
value: x,
}))
console.log(result);