javascript过滤器和切片无法正常工作

时间:2017-09-13 10:42:09

标签: javascript arrays reactjs

我有以下代码:

conditions.slice(0).filter(condition => condition.processsteptemplate === stepId);

它应该从数组中切出一些对象。问题是===不匹配所以所有元素仍然在数组中......

我的失败

更新

示例代码:

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)

1 个答案:

答案 0 :(得分:2)

此处删除了processsteptemplate = 10的数组



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);




你可能做错的是你假设

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

会更改当前数组中的值,这不是真的,它会返回一个新数组,现在存储在结果中,正如您现在可以在上面的代码片段中看到的那样