2

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.

asked Feb 20, 2015 at 13:39

4 Answers 4

1

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));
answered Feb 20, 2015 at 13:45
Sign up to request clarification or add additional context in comments.

Comments

0

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/

answered Feb 20, 2015 at 13:46

2 Comments

Everyone had the right answer, but the little explanation helped me understand. Thank you.
I don't think you require nested loops for this. Check my answer once.
0

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.

answered Feb 20, 2015 at 13:46

Comments

0

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.

felipe.zkn
2,0807 gold badges32 silver badges64 bronze badges
answered Feb 20, 2015 at 13:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.