按多个键过滤JSON数据

问题描述 投票:0回答:2

我的json数据在这里

 [
   {
      "id":"13167",
      "photo_cd":"9946",
      "src":"",
      "price":"5990",
      "hf_section":"1",
      "sell_price":"6469",
      "design_id":"1",
      "genre_id":"1",
      "color_id":"1"
   },
   {
      "id":"13166",
      "photo_cd":"9945",
      "src":"",
      "price":"5990",
      "hf_section":"1",
      "sell_price":"6469",
      "design_id":"3",
      "genre_id":"1",
      "color_id":"1"
   },
   {
      "id":"13165",
      "photo_cd":"9944",
      "src":"",
      "price":"5990",
      "hf_section":"1",
      "sell_price":"6469",
      "design_id":"1",
      "genre_id":"1",
      "color_id":"4"
   },
   {
      "id":"13164",
      "photo_cd":"9943",
      "src":"",
      "price":"6990",
      "hf_section":"1",
      "sell_price":"7549",
      "design_id":"2",
      "genre_id":"3",
      "color_id":"3"
   }
]

我的样子就是这样的

["", "2", "1", "0", "0", "0"]

我的JS就在这里

 $.getJSON(
      filepath,
      function(data) {
    var keys = [];
    var values = [];
     if (param[1] != 0) {
      keys.push("hf_section");
      values.push(param[1]);

     }
     if (param[2] != 0) {
      keys.push("design_id");
      values.push(param[2]);

     }
     if (param[3] != 0) {
      keys.push("genre_id");
      values.push(param[3]);
     }
     if (param[4] != 0) {
      keys.push("color_id");
      values.push(param[4]);
     }
     if (param[5] != 0) {
      keys.push("price");
      values.push(param[5]);
     }
    var result = data.filter(function(e) {
      return keys.every(function(a) {
        return values.includes(e[a])
      })
    })
  })

我想用AND条件过滤所有参数。现在它像OR一样

javascript json filter filtering
2个回答
2
投票

我猜您希望使用param作为字段值列表按以下顺序进行过滤:hf_section,design_id,genre_id,color_id,price。

我假设当param [n]为0时,应该忽略字段值。

在这种情况下,我们可以尝试这种方法:

let testData = [{ id: "13167", photo_cd: "9946", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "1" }, { id: "13166", photo_cd: "9945", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "3", genre_id: "1", color_id: "1" }, { id: "13165", photo_cd: "9944", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "4" }, { id: "13164", photo_cd: "9943", src: "", price: "6990", hf_section: "1", sell_price: "7549", design_id: "2", genre_id: "3", color_id: "3" }];
let fields = ["photo_cd", "hf_section", "design_id", "genre_id", "color_id", "price"];

function filterDataItems(data, filterParams) {
    return data.filter(item => {
        return fields.every((fieldName, index) => {
            return filterParams[index] == 0 || (item[fieldName] == filterParams[index]);
        });
    });
}

console.log('Test 1: ', filterDataItems(testData, ["9946", "1", "1", "1", "0", "5990"]));
console.log('Test 2: ', filterDataItems(testData, ["0", "0", "0", "0", "4", "0"]));
console.log('Test 3: ', filterDataItems(testData, ["0", "0", "0", "0", "0", "0"]));
console.log('Test 4: ', filterDataItems(testData, ["0", "0", "0", "0", "0", "0"]));

2
投票

我建议使用一个对象

filter = {
    hf_section: '2',
    design_id: '1',
    genre_id: '0',
    color_id: '0', 
    price: '0'
}

使用与给定数据的键对应的键和用于过滤的所需值。

然后你可以过滤键只获得想要的结果。

如果您使用数字而不是字符串进行比较,则过滤键并过滤对象可能会更容易。

var data = [{ id: "13167", photo_cd: "9946", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "1" }, { id: "13166", photo_cd: "9945", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "3", genre_id: "1", color_id: "1" }, { id: "13165", photo_cd: "9944", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "4" }, { id: "13164", photo_cd: "9943", src: "", price: "6990", hf_section: "1", sell_price: "7549", design_id: "2", genre_id: "3", color_id: "3" }],
    items = ["", "2", "1", "0", "0", "0"],
    filter = { hf_section: '2', design_id: '1', genre_id: '0', color_id: '0', price: '0' },
    keys = Object.keys(filter).filter(k => +filter[k]),
    result = data.filter(o => keys.every(k => o[k] == filter[k]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
© www.soinside.com 2019 - 2024. All rights reserved.