UTN
UTN

Reputation: 167

Using for loop inside forEach array

Please take a look at the code below.

I have a main array: nop and a temp array: tempArray

If the tempArray array contains elements that are in the main array nop, then mark its isSelect as true.

However, if you run the code below, you will see only the last element of tempArray as been changed on the main array nop....

var nop = [
    {title:'blue', isSelect: false },
    {title:'red', isSelect: true },
    {title:'yellow', isSelect: false },
    {title:'black', isSelect: false },
    {title:'dark blue', isSelect: false },
    {title:'reddish', isSelect: false },
    {title:'hello', isSelect: false },
    {title:'help', isSelect: false },
    {title:'me', isSelect: false }
];

var tempArray = ["blue", "hello", "help"];

tempArray.forEach(function(value){
                var index;
                for (index = 0; index < nop.length; ++index) {
                    if (nop[index].title === value){
                        nop[index].isSelect = true;
                        console.log('FOR LOOP = TRUE for: ' + value);
                    }
                    else {
                        nop[index].isSelect = false;
                    }
                }
            });

console.log(JSON.stringify(nop));

The above results in :

FOR LOOP = TRUE for: blue
FOR LOOP = TRUE for: hello
FOR LOOP = TRUE for: help
[{"title":"blue","isSelect":false},{"title":"red","isSelect":false},{"title":"yellow","isSelect":false},{"title":"black","isSelect":false},{"title":"dark blue","isSelect":false},{"title":"reddish","isSelect":false},{"title":"hello","isSelect":false},{"title":"help","isSelect":true},{"title":"me","isSelect":false}]

Only this element was updated: {"title":"help","isSelect":true}

I want to get all 3 elements updated:

{"title":"blue","isSelect":true} 
{"title":"yellow","isSelect":true}
{"title":"help","isSelect":true}

What am I doing wrong?

Thank you.

Upvotes: 2

Views: 82

Answers (4)

raist
raist

Reputation: 1

While looping with forEach the second loop erases the changes from the first one as your else case set isSelect to false if the title is not the current value.

You should check if isSelected is true in your for block and skip title equals value section.

Upvotes: 0

void
void

Reputation: 36703

Just remove else condition

tempArray.forEach(function(value){
                var index;
                for (index = 0; index < nop.length; ++index) {
                    if (nop[index].title === value){
                        nop[index].isSelect = true;
                        console.log('FOR LOOP = TRUE for: ' + value);
                    }

                }
            });

Working Fiddle

EDIT

for (index = 0; index < nop.length; ++index) 
               if (tempArray.indexOf(nop[index].title)) 
                      nop[index].isSelect = true;

console.log(JSON.stringify(nop));

Upvotes: 1

Ajay Narain Mathur
Ajay Narain Mathur

Reputation: 5466

You are making the unmatched one false in else part. So, only the last one is updated and then not changed. Remove the else part.

Upvotes: 0

Joe Fitter
Joe Fitter

Reputation: 1309

For each colour you are overriding the previous result because of the "else" set them all to false first then loop and set the ones you want to true

        tempArray.forEach(function(value){
            var index;
            for (index = 0; index < nop.length; index++) {
                if (nop[index].title === value){
                    nop[index].isSelect = true;
                    console.log('FOR LOOP = TRUE for: ' + value);
                }

            }
        });

js fiddle - http://jsfiddle.net/rxq529s4/

Upvotes: 0

Related Questions