我希望使用 jQuery 循环访问一个对象并输入到某些索引中。我正在使用 Sweet Alert 2 和链接模式,但我需要动态生成标题。标题位于下面的数组中。
SA2 使用的对象如下:
var steps = [{
title: 'Questions',
input: 'radio',
inputOptions: inputOptions,
}]
我猜方括号后面是某种“每个”。
["Washing Machine diagnosis", "Washing Machine type", "Have you ever had an engineer look at this fault?", "Has an engineer looked at this appliance in the last 6 months?", "Has anybody that is not a qualified repair engineer attempted to repair the Washing Machine?", "Duration of problem", "When did you purchase this Washing Machine?", "Do you have a receipt?"]
本质上我需要创建:
var steps = [{
title: 'Washing Machine diagnosis',
input: 'radio',
inputOptions: inputOptions,
},
{
title: 'Washing Machine diagnosis',
input: 'radio',
inputOptions: inputOptions,
}]
感谢您的帮助
最佳答案
您可以使用Array.map()
ES6
var result = ["Washing Machine diagnosis", "Washing Machine type", "Have you ever had an engineer look at this fault?", "Has an engineer looked at this appliance in the last 6 months?", "Has anybody that is not a qualified repair engineer attempted to repair the Washing Machine?", "Duration of problem", "When did you purchase this Washing Machine?", "Do you have a receipt?"]
.map(e => ({title:e,input:"radio",inputOptions:{}}));
console.log(result)
ES5
var result = ["Washing Machine diagnosis", "Washing Machine type", "Have you ever had an engineer look at this fault?", "Has an engineer looked at this appliance in the last 6 months?", "Has anybody that is not a qualified repair engineer attempted to repair the Washing Machine?", "Duration of problem", "When did you purchase this Washing Machine?", "Do you have a receipt?"]
.map(function(e){
return {title:e,input:"radio",inputOptions:{}};
});
console.log(result)
关于javascript - 使用 jQuery 循环对象并输入某些索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041832/