如何从JS中的两个多维数组返回两个元素

时间:2018-07-20 10:17:59

标签: javascript arrays

我在尝试返回(b [j])以使数组中的两个匹配元素都挣扎,有什么提示吗?

var newArray = []; newArray2 = [];

for (var i = 0; i < a.length; i++) {
    // we want to know if a[i] is found in b
    var match = false; // we haven't found it yet
    for (var j = 0; j < b.length; j++) {
        if (a[i] == b[j]) {
            // we have found a[i] in b, so we can stop searching
            match = true;
            newArray2.push(b[j][1]);
            break;
        }
        // if we never find a[i] in b, the for loop will simply end,
        // and match will remain false
    }
    // add a[i] to newArray only if we didn't find a match.
    if (!match) {
        newArray.push(a[i]);
    } }

2 个答案:

答案 0 :(得分:0)

如果我对您的理解正确:

作为对象返回:

GET / HTTP/1.1
...

或以数组形式返回:

return {newArray: newArray, newArray2: newArray2};

答案 1 :(得分:0)

返回{found:newArray2,notFound:newArray}

请注意,由于将b [j] [1]添加到newArray2但将a [i]添加到newArray(如肯尼在评论中提到的那样),您可能会遇到错字