0

I have project's function as follow: I have a two-dimensional array, then should delete the specific one-dimensional array. The two-dimensional array is:

//ID,Name,Age,Score.Student's ID is unique.

var arr1=[["1","aaa","20","90"],
      ["2","bbb","21","80"],
      ["3","ccc","22","70"], 
      ["4","ddd","23","60"],
     ];

var arr2=["3","ccc","22","70"];

I want to get the a two-dimensional array.The result as follow:

result=arr1=[["1","aaa","20","90"],
      ["2","bbb","21","80"],
      ["4","ddd","23","60"],
     ];

I write some codes,but I get a logic error.The URL of jsfiddle is:Edit fiddle

Thank you very much.

3
  • According to your demo, you are using Underscore, right? Commented Feb 26, 2014 at 15:20
  • 2
    Just a tip: if your ID is unique, use a dictionary/object: it will make much quicker to find/add/delete new items. Commented Feb 26, 2014 at 15:23
  • @MarcoCI My workmate also suggest me to use the object, then implement the add or delete methods for this object.I will try do this later.Thank you Commented Feb 28, 2014 at 6:21

3 Answers 3

2

So you want to delete the array in arr1 with the ID correspoding to the ID in arr2? If thats the problem simply write:

for(var i=0;i<arr1.length;i++){
    if(arr1[i][0] === arr2[0])
        arr1.splice(i, 1);
}

http://jsfiddle.net/2pYT2/3/

Sign up to request clarification or add additional context in comments.

3 Comments

Actually in his answer he's comparing strings, not arrays
I'm not comparing arrays, I'm comparing strings i.e. elements from arrays.
Yeah, I misread that. That still seems wrong though, I think all of the elements in arr1[i] need to match arr2 to remove it, but perhaps I've also misunderstood the question.
0

Since you are using Underscore in your demo:

var result = _.filter(arr1, function (el) {
    return el[0] != arr2[0];
});

Updated demo: http://jsfiddle.net/2pYT2/4/

Comments

0

You can use filter function of Array. See the code below, not tested but hope it helps.

This is to fix your current code. Better is solution is to convert two dimensional array to dictionary as MarcoCI suggested. It will help to you a lot.

var arr1 = [
    ["1", "aaa", "20", "90"],
    ["2", "bbb", "21", "80"],
    ["3", "ccc", "22", "70"],
    ["4", "ddd", "23", "60"], ];
var arr2 = ["3", "ccc", "22", "70"];




// filters comparing all elements
// use filterArray(sourceArray, array1, array2, array3 ...)
function filterArray(arr) {
    var args = arguments;
    return arr.filter(function (x) {
        for (var i = 1; i < args.length; i++) {
            var tarr = args[i];
            for (var j = 0; j < tarr.length && j < x.length; j++) {
                if (tarr[j] != x[j]) {
                    return true;
                }
            }
        }
        return false;
    });

}

console.log(filterArray(arr1, arr2));

// filters comparing 0th element
// use filterArrayID(sourceArray, array1, array2, array3 ...)
function filterArrayID(arr) {
    var args = arguments;
    return arr.filter(function (x) {
        for (var i = 1; i < args.length; i++) {
            var tarr = args[i];
            if (tarr[0] != x[0]) {
                return true;
            }
        }
        return false;
    });

}

console.log(filterArrayID(arr1, arr2));

fiddle : http://jsfiddle.net/2pYT2/5/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.