0

http://jsfiddle.net/0czdrn7t/4/

I try to compare two array ,

find object(Type = 4) in array arrayB and if all object(Type = 4) Privilege not match the arrayA value, then insert to arrayC.

for this example the arrayA[0] is 1 already match in arrayB last object then I don't want it push into the result, want's wrong with my code??

var arrayA = [1];

var arrayB = [ { Type: 1,
        Privilege: 0,
      },
      { Type: 3,
        Privilege: 0,
      },
      { Type: 3,
        Privilege: 1,
      },
      { Type: 4,
        Privilege: 1,
      } ];


var arrayC = [];

var type = 4;
for (var i = 0; i < arrayA.length; i++) {
  var insertValidate = true;
  var issetTypeValidate = false;

  for (var ii = 0; ii < arrayB.length; ii++) {
    if (arrayB[ii].Type == type) {
      if (arrayB[ii].Privilege != arrayA[i]) {
        insertValidate = false;
        // break;
      }
      
      issetTypeValidate = true;
    }
  }

  
  if (issetTypeValidate == true) {
    if (insertValidate == true) {
      var o = {};
      o.Type = type;
      o.Privilege = arrayA[i];
      arrayC.push(o);
    }
  } else {
    // var o = {};
    // o.Type = type;
    // o.Privilege = arrayA[i];
    // arrayC.push(o);
  }
}

console.log(arrayC);

1
  • use alert(JSON.stringify(prepareInsertRoleList)); instead of console.log(prepareInsertRoleList); Commented Dec 9, 2015 at 13:45

2 Answers 2

1

Because updateRoleList[ii].Privilege != inputUserRoleType4[i] returns false when updateRoleList[ii].Privilege equals to 1 and inputUserRoleType4[i] is also 1.

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

Comments

0

try this:

      var arrayA = [1];

    var arrayB = [ { Type: 1,
            Privilege: 0,
          },
          { Type: 3,
            Privilege: 0,
          },
          { Type: 3,
            Privilege: 1,
          },
          { Type: 4,
            Privilege: 1,
          } ];


    var arrayC = [];

    var type = 4;
    for (var i = 0; i < arrayA.length; i++) {
      var insertValidate = true;
      var issetTypeValidate = false;

      for (var ii = 0; ii < arrayB.length; ii++) {
        if (arrayB[ii].Type == type) {
          if (arrayB[ii].Privilege == arrayA[i]) {
//          alert(arrayB[ii].Privilege +'='+arrayA[i])
// if at least one of them is equal, than don't print it
            insertValidate *= false;
            }
        issetTypeValidate = true;
        }
      }

      if (issetTypeValidate == true) {
        if (insertValidate == true) {
          var o = {};
          o.Type = type;
          o.Privilege = arrayA[i];
          arrayC.push(o);
        }
      } else {

      }
    }


    alert(JSON.stringify(arrayC));

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.