0

Trying below code but not getting expected output I am expecting below output

[{"label":"one","value":"1","disabled":true},{"label":"two","value":"2","disabled":true},{"label":"three","value":"3","disabled":false},{"label":"four","value":"4","disabled":false},{"label":"five","value":"5","disabled":false},{"label":"six","value":"6","disabled":true}]

// ---

var A1 = [ {label:"one", value:"1"}, {label:"two", value:"2"},  {label:"six", value:"6"}]; 
var A2 = [ {label:"one", value:"1","disabled":false}, {label:"two", value:"2","disabled":false}, {label:"three", value:"3","disabled":false}, {label:"four", value:"4","disabled":false}, {label:"five", value:"5","disabled":false}, {label:"six", value:"6","disabled":false}]; 

   for(let i = 0 ; i <A2.length; i++){
      for(let j = 0; j < A1.length; j++){
        if(A1[j].value == A2[i].value){
          A2[i].disabled = true;
        }
        else{
              A2[i].disabled = false;                            
        }
      } 
   }

   console.log( JSON.stringify( A2 ) );
5
  • You're over-writing the value of A2[i] each time through the inner loop - so the final value is only ever a comparison of A2[i].value against the value of the value property in the last object in A1. Commented Oct 18, 2018 at 18:03
  • @RobinZigmond Is there another way to handle this Commented Oct 18, 2018 at 18:05
  • Are you trying to do an array intersect by any chance? Based on your variables - A2 explicitly states that disabled is false - so why would it change to true in your expected output? Sorry - just trying to understand your question here. -- Ignore me! Just re-read what your function was doing there... Commented Oct 18, 2018 at 18:06
  • @ShivajiWatekar - see my answer below. Although this is just based on a guess of what you want, based on the desired output - you don't state clearly, so it's possible I'm wrong. Commented Oct 18, 2018 at 18:07
  • could you please give us an explanation of what you are trying to accomplish? Commented Oct 18, 2018 at 19:40

2 Answers 2

1

If I understand correctly what you're trying to do, the following should do it:

for(let i = 0 ; i <A2.length; i++){
   A2[i].disabled = false;
   for(let j = 0; j < A1.length; j++){
     if(A1[j].value == A2[i].value){
       A2[i].disabled = true;
       break;
     }
   } 
}

That is - start off with the false value (no match), and only set it to true if you find a match. (Then break out of the inner loop, because there's no need to continue.)

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

Comments

0

Here is an alternative perspective (jQuery option) if you want it. This is just an idea as I am sure this can be written more elegantly :)

var A1 = [ {label:"one", value:"1"}, {label:"two", value:"2"},  {label:"six", value:"6"}]; 
var A2 = [ {label:"one", value:"1","disabled":false}, {label:"two", value:"2","disabled":false}, {label:"three", value:"3","disabled":false}, {label:"four", value:"4","disabled":false}, {label:"five", value:"5","disabled":false}, {label:"six", value:"6","disabled":false}]; 

var tempA = new Array();
var resultArray = new Array();
$.each(A1,function(datakey,dataval){
    tempA.push(dataval['label']);
});
$.each(A2,function(datakey,dataval){
if ($.inArray(dataval['label'],tempA)>-1) {
    resultArray.push({'label':dataval['label'],'value':dataval['value'],'disabled':true})}
  else {
    resultArray.push({'label':dataval['label'],'value':dataval['value'],'disabled':false})
  }
});

console.log(resultArray);

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.