[英]Array with values from another array
How can I obtain something like this: 我如何获得这样的东西:
items = [{
value: 'All',
checked: false
}, {
value: 'a',
checked: false
}, {
value: 'b',
checked: false
}, {
value: 'c',
checked: false
}];
eg.: console.log(items[2].value)='b'
where 'a', 'b', 'c' .. come from an array arr = ['a', 'b', 'c', 'd', 'e', ...]
其中'a','b','c'..来自数组arr = ['a', 'b', 'c', 'd', 'e', ...]
I tried with a for 我尝试了一个
for (var i = 0; i < arr.length; i++) {
items=[
{value: 'All', checked: false},
{value: arr[i], checked: false}
]
}
but this is not working and I have no other idea. 但这是行不通的,我没有其他想法。 Is there another way to obtain this? 是否有另一种方法来获得此? Thank you for your time! 感谢您的时间!
You can use Array.map()
: 您可以使用Array.map()
:
const arr = ['all', 'a', 'b', 'c', 'd', 'e']; const result = arr.map(value => ({ value, checked: false })); console.log(result);
You can try following 您可以尝试关注
var items = [{value: 'All', checked: false}];
for (var i = 0; i < arr.length; i++) {
items.push({value: arr[i], checked: false})
}
Is this something like this that you seek ? 您是否正在寻找这样的东西?
const arr = [ 'a', 'b', 'c', 'd', ]; const final = [{ value: 'All', checked: false, }, ...arr.map(x => ({ value: x, checked: false, })), ]; console.log(final);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.