javascript - How can I slice out all of the array objects containing a value and storing them in a new array -


so have array , want make new array objects have swimming value in sports.

 var watchesarray = [         {           model: "swim",            image:"",           price: 149.99,           sports:["swimming", "running"]         },         {           model: "fr 10",            image:"",           price: 129.99,           sports:["running"]          },         {           model: "fr 15",            image:"",           price: 199.99,           sports:["running"]          },     ]; 

so far have dont know how add on sliced array each go around in loop. how should this?

 (var = 0; < watchesarraylength; i++) {         if (watchesarray[i].sports.indexof("swimming") > -1) {             var runningwatcharray = watchesarray.slice(i);          }      } 

you can use .filter() method:

watchesarray = [...];  var result = watchesarray.filter(function(watch) {     return watch.sports.indexof('swimming') !== -1; }); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -