javascript - Object Splicing within Array not giving correct result -
javascript - Object Splicing within Array not giving correct result -
in application want splice objects array upon matching, using lodash function splicing shown below, unfortunately json not splicing correctly,
working demo
can give me suggestion issue
var arr = [{ name: 'jack', id: 125 }, { name: 'jack', id: 125 }]; var result = _.without(arr, _.findwhere(arr, {name: 'jack'})); console.log(json.stringify(result));
expected result
[]
actual result
[{"name":"jack","id":125}]
update 1
even using normal javascript way giving same output
for(var = 0; < arr.length; i++) { var obj = arr[i]; if(obj.name === 'jack') { arr.splice(i, 1); } }
#1 var arr = [{ name: 'jack', id: 125 }, { name: 'jack', id: 125 }]; var result = _.rest(arr, function (el) { homecoming el.name === 'jack'; }); console.log(json.stringify(result)); // "[]" #2 var arr = [{ name: 'jack', id: 125 }, { name: 'jack', id: 125 }, { name: 'foo', id: 124 }]; var result = _.rest(arr, function (e) { homecoming e.name === 'jack'; }); console.log(json.stringify(result)); // "[{\"name\":\"foo\",\"id\":124}]" // 3 can utilize _.filter if not want piece of array... var result = _.filter(arr, function (e) { homecoming e.name !== 'jack'; }); console.log(json.stringify(result)); // "[{\"name\":\"foo\",\"id\":124}]"
javascript lodash splice
Comments
Post a Comment