【问题标题】:Jquery response, only single object returned while loop failsJquery响应,循环失败时仅返回单个对象
【发布时间】:2020-09-30 20:37:25
【问题描述】:

我正在尝试从 json 查询中返回一个姓氏数组,以构成用户可以选择的部分的一部分。

$.ajax({
url: 'https://reqres.in/api/users?page=1',
type: "GET",
success: function (response) {
    console.log(response);
    original = convState.current;
    State.current.next = State.newState({
       type: 'select',
       question: ['Please select'],
       answer: [{text: [response.data[0].last_name], value: 'true'}],
});

当前结果只返回https://reqres.in/api/users?page=1的json中的一个姓氏bluth

如果我尝试添加一个循环来访问数组,绝对不会发生任何事情。 html 页面只是等待某件事发生,而这永远不会发生。 可以请一些人帮助纠正循环,我不知道我应该在哪里以及如何添加它。我在这里尝试过,但页面等待什么都没有发生。

    var len = data.length;
      for(var i=0;i<len;i++){
        answer: [{text: [response.data[i].last_name], value: 'true'}],
    }

【问题讨论】:

    标签: jquery json ajax


    【解决方案1】:

    我会检查您的控制台日志以查看是否发生任何错误。提供的for 循环对我不起作用。但是,AJAX 查询有效,我可以通过修改您的 for 循环来遍历数组。看看我的示例代码。

    编辑:我了解您的代码中发生了什么。您尝试在创建对象时实现for 循环(这不起作用)。我的示例代码将所有姓氏聚合到一个数组中,然后使用它来创建新状态。我不确定你是想要多个新状态,还是想要一个有多个答案的状态。

    $.ajax({
        url: 'https://reqres.in/api/users?page=1',
        type: "GET",
        success: function (response) {
            let data = response.data;
            let array = [];
            for(var i = 0; i < data.length; ++i) {
                let account = data[i];
                let info = {text: [account.last_name], value: 'true'};
                array.push(info);
            }
            console.log(array);
            // Now do something with the array...
            original = convState.current;
            State.current.next = State.newState({
                type: 'select',
                question: ['Please select'],
                answer: array
            }
    });
    

    或者如果您想使用map 函数来简化数组创建,您可以执行以下操作。

    $.ajax({
        url: 'https://reqres.in/api/users?page=1',
        type: "GET",
        success: function (response) {
            let data = response.data;
            let array = data.map((account) => Object.create({text: [account.last_name], value: 'true'}));
            console.log(array)
            // Now do something with the array...
            original = convState.current;
            State.current.next = State.newState({
                type: 'select',
                question: ['Please select'],
                answer: array
            }
        }
    });
    

    【讨论】:

    • 谢谢。让我快速将其修改为我的代码,看看它是如何工作的!只是一个稍微偏离主题的问题,我可以在变量中预定义last_name 并将其用作let info = {text: [account.(var_name_here)] 而不必硬编码last_name
    • 它成功了!!谢谢伙计,请在我的问题中使用屏幕截图查看编辑。如果您对该变量有任何想法,那将是一个救命稻草。
    • 我相信我明白你的意思。如果要动态访问对象中的属性,请使用带有字符串作为键的索引器。例如:account.last_name 变为 account["last_name"]。这些是等价的。查看此链接了解更多信息:dmitripavlutin.com/access-object-properties-javascript
    • 谢谢。所以我想做的是使用 html 来询问用户他想要什么。因此,如果用户随后插入last_name,它将显示姓氏列表,如果用户定义first_name,它应该定义收集那个。但我认为它变得更加复杂,因为如果用户然后选择一个姓氏,则 ajax 变量应该更改为新值。假设smith 在提交时将获得ajax 查询的另一部分。所以非常需要更新 ajax 调用需要从链接中收集的内容。换句话说,last_name 不应硬编码为 [account.last_name]
    • 我想这是一个相当大的问题,所以我需要问一个新问题。我会在这里链接它。
    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2012-01-03
    • 2013-03-21
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多