我正在使用Bootstrap Select Picker插件,该插件允许(与普通select控件相同)选择多个值。
如果我调用:$('#items).val()
,我将在选择控件中选择一个值数组:
["1", "2"]
我想将每个值包含在另一个对象中,例如:
var main = {
items: $('#items').val()
}
如何也可以显示这样的结构:
[ { id: 1 }, {id: 2} ]
答案 0 :(得分:2)
使用地图方法并将其更改为对象
public class NullableTimePickerRenderer : TimePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
{
base.OnElementChanged(e);
var timePicker = (UIDatePicker)Control.InputView;
timePicker.Locale = new NSLocale("no_nb");
if (e.NewElement != null && this.Control != null)
{
this.UpdateDoneButton();
this.AddClearButton();
this.Control.BorderStyle = UITextBorderStyle.Line;
Control.Layer.BorderColor = UIColor.LightGray.CGColor;
Control.Layer.BorderWidth = 1;
if (Device.Idiom == TargetIdiom.Tablet)
{
this.Control.Font = UIFont.SystemFontOfSize(25);
}
}
}
private void UpdateDoneButton()
{
var toolbar = (UIToolbar)Control.InputAccessoryView;
var doneBtn = toolbar.Items[1];
doneBtn.Clicked += (sender, args) =>
{
NullableTimePicker baseTimePicker = this.Element as NullableTimePicker;
if (!baseTimePicker.NullableTime.HasValue)
{
baseTimePicker.AssignValue();
}
};
}
private void AddClearButton()
{
var originalToolbar = this.Control.InputAccessoryView as UIToolbar;
if (originalToolbar != null && originalToolbar.Items.Length <= 2)
{
var clearButton = new UIBarButtonItem("clear", UIBarButtonItemStyle.Plain, ((sender, ev) =>
{
NullableTimePicker baseTimePicker = this.Element as NullableTimePicker;
this.Element.Unfocus();
this.Element.Time = DateTime.Now.TimeOfDay;
baseTimePicker.CleanTime();
}));
var newItems = new List<UIBarButtonItem>();
foreach (var item in originalToolbar.Items)
{
newItems.Add(item);
}
newItems.Insert(0, clearButton);
originalToolbar.Items = newItems.ToArray();
originalToolbar.SetNeedsDisplay();
}
}
}
答案 1 :(得分:1)
items.map(id => ({id: Number(id)}))
答案 2 :(得分:0)
该映射遍历每个对象,并在数组中存储一个对象以及每个已访问元素的ID,然后将其打印出来
var a=["1", "2"];
console.log(a.map((e)=>({id:Number(e)})))
答案 3 :(得分:0)
使用此:
let array = ["1","2"];
console.log(array.map((x)=> {return{id:Number(x)}}));
答案 4 :(得分:0)
使用map
var items = $('#items').val()
console.log(items);
// ['1', '2']
var main = {
items: items.map(function(item) {
return { id: parseInt(item, 10) }
})
}
console.log(main.items);
// [ { id: 1 }, {id: 2} ]