简体   繁体   English

如何从对象的数组中挑选数据并将其插入Javascript中的对象的另一个数组中?

[英]How can I pick data out of an array in a object and insert it into another array in an object in Javascript?

I have variable qstore that's has an ans array containing a list of responses. 我有一个变量qstore,它的ans数组包含响应列表。 The field named r is used to hold the response data (true or false) and is stored in an array called ans 名为r的字段用于保存响应数据(对或错),并存储在称为ans的数组中

qstore: {"id":2,
         "qId":2,
         "ans":[{"r":true},
                {"r":true},
                {"r":false},
                {"r":false},
                {"r":false}]}

Another variable qview: 另一个变量qview:

qview: {"problemId":2,
          "questionId":1,
          "answer":null,
          "text":"xx",
          "answers":[{"answerId":1,
                      "response":false},
                     {"answerId":2,
                      "response":false},
                     {"answerId":3,
                      "response":false},
                     {"answerId":4,
                      "response":false},
                     {"answerId":5,
                       "response":false}]}

What I need to do is IF there is an array called ans in qstore (there may not be one) then I need to take the answer responses field r and use that to update the answers response field in the qview object. 我需要做的是,如果在qstore中有一个称为ans的数组(可能没有一个),那么我需要获取答案响应字段r并使用它来更新qview对象中的答案response字段。 Note that the qview and qstore if they do have answers will always have the same number of answers. 请注意,如果qview和qstore确实有答案,则它们将始终具有相同数量的答案。

Can anyone tell me a simple way that I can do this? 谁能告诉我我可以做到的简单方法?

What I need to do is IF there is an array called ans in qstore (there may not be one) then I need to take the answer responses field r and use that to update the answers response field in the qview object. 我需要做的是,如果qstore中有一个称为ans的​​数组(可能没有一个),那么我需要获取答案响应字段r并使用它来更新qview对象中的答案响应字段。 Note that the qview and qstore if they do have answers will always have the same number of answers. 请注意,如果qview和qstore确实有答案,则它们将始终具有相同数量的答案。

// assuming `qstore`, `qview`
var i, j, ans = qstore.ans;
if (ans) { // if qstore has non-falsy ans
    qstore: for (i = 0, j = 0; i < ans.length; ++i) { // loop over ans
        for (; j < qview.answers.length; ++j) { // look in qview
            if (qview.answers[j].answerId === i + 1) { // qview has already
                qview.answers[j].response = ans[i].r;
                continue qstore; // go back to looping ans
            } else if (qview.answers[j].answerId <= i) { // qview needs new
                break; // get out of qview loop
            }
        } // if we reach here then qview didn't have an entry for this item
        qview.answers.splice(j, 0, { // insert new entry
            'answerId': i + 1,
            'response': ans[i].r
        });
    }
}

Just simply loop over the qstore.ans array (if it exists) and set the respective value in qview . 只需简单地循环遍历qstore.ans数组(如果存在)并在qview设置各自的值。

if(qstore.hasOwnProperty('ans')){
    for(var i = 0, len = qstore.ans.length; i < len; i++){
        qview.answers[i].response = qstore.ans[i].r;
    }
}

The most simple solution. 最简单的解决方案。

This way it will execute only if you have at less, 1 register on ans array , i see that the solutions above do not verify if the ans array have registers, so i think it is the right one for your case. 这样一来,只有在ans数组上只有1个寄存器时,它才会执行 ,我看到上述解决方案无法验证ans数组是否具有寄存器,因此我认为这是适合您的情况的一个方法。

Here is the code: 这是代码:

if (qstore.ans != null){
    if (qstore.ans.length > 0){
        for (i=0;i<qstore.ans.length);i++){
            qview.answers[i].response = qstore.ans[i].r;
        }
    }
}

JavaScript has an multiple ways to check if a key exists in an object: in and Object.hasOwnProperty . JavaScript有多种方法来检查对象中是否存在键: inObject.hasOwnProperty The difference between the two is that in also returns true if the key is found in the prototype chain of your object. 两者之间的区别在于,如果在对象的原型链中找到了密钥, in也将返回true

Now it appears the id's of your response are 1-indexed, but that doesn't matter if we iterate over the index of its position in the array itself: 现在,您的响应的ID似乎是1索引的,但是如果我们迭代其在数组本身中的位置的索引就没有关系了:

if ('ans' in qstore) {
    for (var i = 0; i <= qstore.ans.length; i++) {
        qview.answers[[i].response = qstore.ans[i].r
    }
}

There's also a nicer forEach available, if you're not expecting to support IE8 and earlier, or are prepared to insert a "shim": 如果您不希望支持IE8和更早版本,或者准备插入“ shim”,也可以使用forEach更好的方法:

if ('ans' in qstore) {
    qstore.ans.forEach(function(element, index) {
        element.response = qstore.ans[index].r
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM