0

I have an array of objects in JavaScript. e.g. current_films[0].f_name, current_films[0].f_pattern etc. I want to copy the array into another similiar to the following:

for(var i=0; i<current_films.length; i++)
    {
            if(current_films[i].f_material == Value)
                {
                    temp[i] = current_films[i];
                }
    }

However, there seems to be an inexplicable problem when I do this. By inexplicable problem, I mean that the code does not execute and the array is not copied as I desire.

Any help would be greatly appreciated. Thank you!

P.S. Could you please mention why the above code would not work? As in, if I put an alert("Reached here");, it's not getting executed. Any ideas why its so?

5
  • try javascript for in loop for object Commented Oct 12, 2013 at 16:24
  • 2
    What is Value? Can you give us a sample data that you expect to get? Commented Oct 12, 2013 at 16:26
  • Your temp array will be sparse -- the indexes will not be sequential from 0, only the indexes from current_film that match Value will exist. Is that the problem? Commented Oct 12, 2013 at 16:30
  • Value is just a string. Right now, I am just trying to copy all those objects whose f_material attribute matches Value Commented Oct 12, 2013 at 16:31
  • There is no such thing as a "JSON object" Commented Oct 12, 2013 at 16:36

2 Answers 2

2

One problem I see is that you set temp[i] to the value which means there would be gaps in the temp array. You could use push() to append the value to temp so you don't need to manage two sets of indices.

You could also use JavaScript's Array.filter() to do this a little easier. Filter will return a new array of the values from the original array where your function returns true.

var temp = current_films.filter(function(film) {
  return (film.f_material === Value);
});
Sign up to request clarification or add additional context in comments.

4 Comments

consider cloning if required.
@nkron I would use === instead of ==
Yeah, that would be better. I've made that change.
Thanks @nkron! This worked! Do you have any idea why the code I wrote was not allowing an alert("Reached here"); to execute?
0

P.S. Could you please mention why the above code would not work? As in, if I put an alert("Reached here");, it's not getting executed. Any ideas why its so?

I'd guess f_material is not defined for every element in the array.

If that's the case I'd use

if(typeof(current_films[i].f_material)!=='undefined')
{
    if(current_films[i].f_material == Value)
    {
        temp[i] = current_films[i];
    }
}

Anyway I'd suggest you to get familiar with the browser's javascript debugger (assumed that code runs in a browser)

Finally note that you're not copying the array/object:

temp[i] is a reference to current_films[i]

Modifying current_films later in the code will result in modifying temp

If that's not the behaviour desired Google for "javascript object copy".

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.