如何使用JavaScript过滤JSON

时间:2019-06-28 03:40:44

标签: javascript json object

我需要根据其值过滤一个巨大的JSON文件。

这是我的JSON代码(这是我的JSON对象的一部分,实际上,它有1000多个)。

var jsonObject = [  
   {  
      "UserId":10259,
      "FullName":"hello world",
      "CustomerId":"10165"
   },
   {  
      "UserId":10405,
      "FullName":"test value",
      "CustomerId":"10261"
   },
   {  
      "UserId":10400,
      "FullName":"mark ant",
      "CustomerId":"10161"
   },
   {  
      "UserId":16224,
      "FullName":"jhon cena",
      "CustomerId":""
   },
   {  
      "UserId":10416,
      "FullName":"shoze ahh",
      "CustomerId":"1"
   },
   {  
      "UserId":10244,
      "FullName":"kajal man",
      "CustomerId":"10"
   }
];

要过滤上述JSON对象,我使用了以下过滤器功能。

function usersBasedOnIDs(CustomerIds) {
    if (CustomerIds == "") {
        console.log(jsonObject);
    } else if (CustomerIds == "9999xx") {

        let result = jsonObject.filter(c => c.CustomerId == "");
        console.log(result);
    } else {
        let result = jsonObject.filter(c => c.CustomerId != "" && CustomerIds.includes(c.CustomerId));
        console.log(result);
    }   
}

我如何调用函数

usersBasedOnIDs("");
usersBasedOnIDs("10261,10165");
usersBasedOnIDs("9999xx"); 

我的代码有几个问题,

  1. 此功能不适用于早期版本的IE 11

  2. 另一件事是,当我将函数调用为usersBasedOnIDs("10261,10165");usersBasedOnIDs("10261");时,该函数的返回结果与JSON输出有关

    {UserId: 10405, FullName: "test value", CustomerId: "10261"} {UserId: 10416, FullName: "shoze ahh", CustomerId: "1"} {UserId: 10244, FullName: "kajal man", CustomerId: "10"}

但是我的期望是,只有这个

{UserId: 10405, FullName: "test value", CustomerId: "10261"}

如何更改功能以避免这两个问题

4 个答案:

答案 0 :(得分:3)

字符串"10261,10165"包括10261以及字符串110。这就是为什么您还要过滤这些ID的原因。

相反,您可以split,处的字符串以获取客户ID数组,然后使用Array#includes

const array=[{"UserId":10259,"FullName":"hello world","CustomerId":"10165"},{"UserId":10405,"FullName":"test value","CustomerId":"10261"},{"UserId":10400,"FullName":"mark ant","CustomerId":"10161"},{"UserId":16224,"FullName":"jhon cena","CustomerId":""},{"UserId":10416,"FullName":"shoze ahh","CustomerId":"1"},{"UserId":10244,"FullName":"kajal man","CustomerId":"10"}]

function usersBasedOnIDs(CustomerIds) {
    if (CustomerIds === "")
        return array;
    
    if (CustomerIds == "9999xx")
      return array.filter(c => c.CustomerId == "")
    
   const ids =  CustomerIds.split(",");
   return array.filter(c => c.CustomerId !== "" && ids.includes(c.CustomerId));
}

console.log(usersBasedOnIDs("10261,10165"))
console.log(usersBasedOnIDs("10261"))

IE11不支持箭头功能String#includesArray#includes。您可以将箭头功能更改为:

array.filter(function(c) {
  return c.CustomerId == ""
})

还有

Array#includesString#includes使用polyfill

答案 1 :(得分:2)

因此,这是基于@adiga答案的IE 11的完整答案。将箭头功能更改为普通功能,并使用Array.indexOf代替includes

const array = [
  { UserId: 10259, FullName: "hello world", CustomerId: "10165" },
  { UserId: 10405, FullName: "test value", CustomerId: "10261" },
  { UserId: 10400, FullName: "mark ant", CustomerId: "10161" },
  { UserId: 16224, FullName: "jhon cena", CustomerId: "" },
  { UserId: 10416, FullName: "shoze ahh", CustomerId: "1" },
  { UserId: 10244, FullName: "kajal man", CustomerId: "10" }
];

function usersBasedOnIDs(CustomerIds) {
  if (CustomerIds === "") return array;

  if (CustomerIds == "9999xx") {
    return array.filter(function(c) {
      return c.CustomerId == "";
    });
  }

  const ids = CustomerIds.split(",");
  return array.filter(function(c) {
    return c.CustomerId !== "" && ids.indexOf(c.CustomerId) > -1;
  });
}

console.log(usersBasedOnIDs("10261,10165"))

答案 2 :(得分:1)

1)IE 11不支持箭头功能

尝试将功能更改为

var result = jsonObject.filter(function(c) { return c.CustomerId === ""; });

答案 3 :(得分:1)

您的代码无法在IE 11上运行的原因是它使用include函数检查数组是否包含值。 IE 14及更高版本支持includes function。 IE 11及更早版本也不支持arrow functions

您的代码由于几个错误而无法正常工作。首先,箭头函数回调需要一个return语句。所以代替:

c => c.CustomerId == ""

使用:

c => return(c.CustomerId ==“”)

此外,客户ID应该作为数组而不是字符串传递。因此,不要使用:

usersBasedOnIDs("10261,10165");

您需要使用:

usersBasedOnIDs([10261,10165]);