0

I have an array like this

"chapters":[
            {
                "_id": "5422c6ba0f6303ba22720e14",
                "chapter_name": "Morbi imperdiet libero sed.",
                "sub_title": "Nam id elit tincidunt amet.",
                 "mystatus": "noidea"   

            },
            {
                "_id": "5422bd0d5cb79ae6205b2f6a",
                "chapter_name": "Donec id accumsan volutpat.",
                "sub_title": "Cras volutpat velit nullam.",
                 "mystatus": "noidea"

            },
            {
                "_id": "5423ea5f17c83ad41da1765e",
                "chapter_name": "Donec id accumsan volutpat.",
                "sub_title": "Cras volutpat velit nullam.",
                 "mystatus": "noidea"

            }

        ],

And I have another one

"currentstat": [
        "5423ea5f17c83ad41da1765e",
        "5422c6ba0f6303ba22720e14",
        "5422bd0d5cb79ae6205b2f6a"
    ],

I want to check any of currentstat array value contains in chapters._id.

And I tried like this.

for (var i = 0; i < chapters.length; i++) {
  var attenId = currentstat.toString();
  if (attenId.indexOf(chapters[i]._id) != -1) {
  }
}

I could not get the result without

var attenId = currentstat.toString();

one more thing I need on this.

I need to assign these status array value to corresponding chapters array as per currentstat array value.

status: [ 'passed', 'passed', 'attending']

the whole code is like this

for (var i = 0; i < chapters.length; i++) {
  var attenId = currentstat.toString();
  if (attenId.indexOf(chapters[i]._id) != -1) {
    allChapters[i].mystatus = (status[i]) ? status[i] : 0;
  }
}

but the value does not assign to corresponding _id Please help!

2 Answers 2

4

Example:

var currentstat = ["1","2","3"];
var chapters = [{_id:"2"}, {_id:"4"}, {_id:"6"}];

var objects = chapters.filter(function(chapter) {
    return ~currentstat.indexOf(chapter._id);
});

Just replace those arrays with your data and objects will contain the objects that has same id (in this particular example it is [{_id:"2"}].

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

Comments

1

The crudest of methods is 2 for loops one inside the other:

for (var i = 0; i < currentstat.length; i++) {
    for (var j = 0; j < chapters.length; j++) {
        if (currentstat[i] === chapters[j]._id){
            alert('found it at: ' + i + ' ' + currentstat[i]);
        }
    }
}

check this fiddle

a bit less crude method would be:

for (var i = 0; i < chapters.length; i++) {
    if (currentstat.indexOf(chapters[i]._id) >= 0) {
        alert('found ' + currentstat[i] + ' at index ' + i);
    }
}

fiddle here.

3 Comments

Using indexOf would probably be better than iterating on each iteration.
However, you would need to apply it on chapters not on currentstat
Just submitted an edit that does that and makes sure to let falsy index 0 pass evaluation.

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.