Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
try javascript for in loop for object –  Akshay Khandelwal Oct 12 '13 at 16:24
2  
What is Value? Can you give us a sample data that you expect to get? –  Ionică Bizău Oct 12 '13 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? –  Barmar Oct 12 '13 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 –  Saket Jain Oct 12 '13 at 16:31
 
There is no such thing as a "JSON object" –  Alnitak Oct 12 '13 at 16:36
add comment

2 Answers

up vote 2 down vote accepted

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);
});
share|improve this answer
 
agree that! good you pointed out Array.filter –  Akshay Khandelwal Oct 12 '13 at 16:34
 
consider cloning if required. –  Prongs Oct 12 '13 at 16:37
1  
@nkron I would use === instead of == –  Sriharsha Oct 12 '13 at 16:48
 
Yeah, that would be better. I've made that change. –  nkron Oct 12 '13 at 16:55
 
Thanks @nkron! This worked! Do you have any idea why the code I wrote was not allowing an alert("Reached here"); to execute? –  Saket Jain Oct 12 '13 at 17:02
add comment

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".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.