javascript - 使用 jQuery 循环对象并输入某些索引

标签 javascript jquery arrays ajax loops

我希望使用 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/

相关文章:

javascript - 从 webview 获取用户触摸的元素

javascript - 如何指定哪些 Require.js 模块是用优化器压缩的,哪些不是?

javascript - 光线转换作为碰撞检测器 : not accurate

c - 如何使用位图二维数组存储图的邻接矩阵

java - 从数据包 header 设置 Java 字节数组大小

javascript - 为什么 JS 预加载看起来比加载常规图像引用花费的时间更长?

javascript - 在 JQuery 动态点击处理程序中使用 Ajax 回调变量值

jquery - 旋转设备方向时,JSSOR slider 无法在 iOS 中正确调整大小

c# - Array.Length 与 Array.Count

javascript - 使用 jQuery Ajax 发送 'pure' JSON 而不是多种形式