admin管理员组文章数量:1405627
Given:
let input = [0, 1, 2,,,,,7]
I want to get:
let output = [, 1, 22, 333, 4444, 55555, 666666, 7777777]
(i.e. value equal key times the key)
How can I map
(or equivalent) input including empty values?
If I do input.map()
I will get [, 1, 22,,,,,7777777]
because map
will -of course- not iterate empty values. Data can be arbitrary, strings, objects, numbers, etc. Using numbers in this example for simplicity.
Given:
let input = [0, 1, 2,,,,,7]
I want to get:
let output = [, 1, 22, 333, 4444, 55555, 666666, 7777777]
(i.e. value equal key times the key)
How can I map
(or equivalent) input including empty values?
If I do input.map()
I will get [, 1, 22,,,,,7777777]
because map
will -of course- not iterate empty values. Data can be arbitrary, strings, objects, numbers, etc. Using numbers in this example for simplicity.
-
Does it have to be
input = [0, 1, 2,,,,,7]
or it it possible to simply useArray.from({ length: 8 }, (_, index) => index)
instead, in your use-case, or justArray.from({ length: 8 }, (_, index) => String(index).repeat(index))
to begin with? – Sebastian Simon Commented Jul 27, 2020 at 3:00 -
“Data can be arbitrary, strings, objects, numbers, etc.” — so what’s the algorithm for “arbitrary” data? You assume that integers can be filled in in the missing spots. What’s the expected result for
[1, 2, 3, , , 99]
? What’s the expected result for[1, 2, "a", 99]
? – Sebastian Simon Commented Jul 27, 2020 at 3:07 -
@user4642212 The problem is not the map result. But rather how to "map" (or whatever) empty values. For example
[1,,1]
could yield to[{...}, {...}, {...}]
. – adelriosantiago Commented Jul 27, 2020 at 3:14 -
So you want
arr.fill().map(...)
? – Unmitigated Commented Jul 27, 2020 at 3:15 -
i would suggest something like this
[0, 1, 2, 3, 4, 5].map((el, index) => Array(el).fill(null).map(() => index).join(""))
– Teiem Commented Jul 27, 2020 at 3:21
3 Answers
Reset to default 7You can fill
the array before applying a mapping function so that all indexes are iterated over, including empty ones.
let result = input.fill().map((_,index)=>{/*...*/});
The mapping function provided as the second argument to Array.from
can be similarly used.
let result = Array.from({length: input.length}, (_, index)=>{/*...*/});
Spread syntax does not ignore empty slots, so we can create a copy of the array with spread syntax and map
over that. This has an added advantage of having the elements at each index available as well.
let result = [...input].map((elem, idx)=>{/*...*/});
map() can't include empty values. w3schools
map() does not execute the function for array elements without values.
So, either iterate the array with a for-loop:
out = [];
for (var i = 0; i < input.length; i++) {
var item = input[i];
if (item != null)
out[i] = item.toString().repeat(item);
}
Or create another mapx() functor:
Array.prototype.mapx = function(fn) {
var out = [];
for (var i = 0; i < this.length; i++) {
var item = this[i];
// if (item == null) item = sherlockHolmesKnow();
out[i] = fn(item, i);
}
return out;
};
and apply on the input:
input.mapx( (val, index) => (val == null ? null : (val+'').repeat(val)) )
which yields ["", "1", "22", null, null, null, null, "7777777"].
Let's skip the AI part of clever guessing the numbers in the empty slots.
Here is another approach too verbose but it works, with for loop you can loop through null values
arr=[0, 1, 2,,,,,7]
res=[]
f=false
for(let i=0;i<arr.length-1;i++){
if(arr[i]!=undefined){
res.push(Number(String(arr[i]).repeat(arr[i])))
//f=false
}
if(arr[i]==undefined&&f==false){
n= arr[i-1]
f=true
res.push(Number(String(n+1).repeat(n+1)))}
if(f==true && arr[i]==undefined){
n=Number(res[res.length-1].toString().charAt(0))+1
res.push(Number(String(n).repeat(n)))
}
}
console.log(res)
本文标签: javascriptMap an array to set empty elementsStack Overflow
版权声明:本文标题:javascript - Map an array to set empty elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744336783a2601236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论