JavaScript - 如何基于字符串过滤数组

问题描述 投票:-3回答:1

我有一个像这样的对象:

nameArray = dataArray.map(function (item) {
  return {
    value: item.Title,
    title: item.Title,
    id: item.Id,
    location: item.Location,
    telephone: item.Telephone,
    address: item.Address,
    postcode: item.Postcode,
    category: item.Category
  };
});

然后我做了一些过滤来过滤掉关闭的位置并返回一个最终的数组进行处理:

var CClose = "C Closed";
var Cclosed = "closed";
var CClosed = "C closed";

//for each item if its closed remove it
//push it to standard list 
standardAZList = $.map(nameArray, function (val, key) {
  if (val.category != CClosed || val.category != Cclosed ||
    val.category != CClose)
    return val;
});

for (var i = 0; i < standardAZList.length; i++) {
  //filteredAZList.push(standardAZList[i]);
  alert("After removing name count is inside remove:" +
    standardAZList.length);
}

但是我得到的错误数量是160,但过滤器应该只显示110(有些是类别中的null并不重要)。

javascript arrays for-loop
1个回答
0
投票

在上面的有用评论后做了这个:

//Filter first
let filteredAZLista = nameArray.filter(
c => c.category !== "C closed" && 
c.category !== "closed" && 
c.category !== "C Closed"); 

alert("" + filteredAZLista.length); 

//Loop through
//And Push out
for(var i=0;i<filteredAZLista.length;i++){ 
     standardAZList.push(filteredAZLista[i]); 
    alert("After removing name count is inside remove:" + standardAZList.length); 
}
© www.soinside.com 2019 - 2024. All rights reserved.