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
Post a Comment