Stack Exchange
javascript filter and slice does not work properly
I have the following code:
conditions.slice(0).filter(condition => condition.processsteptemplate === stepId);

It should slice some objects out of an array. Problem is that the === does not match so all elements are still in the array ...
whats  my fail
UPDATE:
Sample Code:


var conditions = [{
      id: 14,
      conditiontype: 1,
      processsteptemplate: 9,
      deleted: false,
      processTemplate_id: 0
    },
    {
      id: 15,
      conditiontype: 1,
      processsteptemplate: 9,
      deleted: false,
      processTemplate_id: 0
    },
    {
      id: 16,
      conditiontype: 1,
      processsteptemplate: 10,
      deleted: false,
      processTemplate_id: 0
    }
  ],
  stepId = 9;
  
const output = conditions.slice(0).filter(condition => condition.processsteptemplate === stepId);
console.log(output)




Why conditions.slice(0).filter? filter will anyways return another array.

just remove slice?

try var output = conditions.filter(x => x.processsTemplate === stepId); console.log(output). Point is, filter will never manipulate your original array. It will return a list of filtered values

already tried ... same result

slice has no effect here as filter won't alter the conditions array but instead returns a new one: var metConditions = conditons.filter...

added the input above

What is your expected output?

expected output is that the one with processsteptemplate=10 is removed

@Felix I just wrapped your code in a snippet and logged the output. Please check and specify, what is incorrect/missing

missing was I haven't assigned the response to a new var

it works well for me what is the problem I dont get it? Also why you use slice?

array#filter doesn't alter the array, it just return a subset of items satisfying the condition.

at all guys thanks for downvote!

here the array with processsteptemplate = 10 is removed


let arr = [
{id: 14, conditiontype: 1, processsteptemplate: 9, deleted: false, processTemplate_id: 0},
{id: 15, conditiontype: 1, processsteptemplate: 9, deleted: false, processTemplate_id: 0},
{id: 16, conditiontype: 1, processsteptemplate: 10, deleted: false, processTemplate_id: 0}
]

let step = 9;

let result = arr.filter((e) => e.processsteptemplate === step);

console.log(result);



What you were probably doing wrong was that you were assuming 
arr.filter((e) => e.processsteptemplate === step);

would change the value in the current array, that is not true, it would return a new array which is now stored in result as you can now see in the above code snippet

To be honest, I addressed this in comments already. Also, questions like this can be resolved if you read the documentation. So answering them, is something, inn my POV, wrong. I'll still take my vote, as it does answer the question.
