如何基于req.query.params过滤json对象

时间:2018-11-16 12:39:10

标签: javascript node.js json

我正在使用node.js并表示,需要过滤掉json对象数组的结果

这是我的对象数组

[ { id: 1, name: "Person 1", boughtItems: { item: "Shoes", currency: "GBP", cost: 200 }, { item: "Bag", currency: "EUR", cost: 300 } }, { id: 2, name: "Person 2", boughtItems: { item: "Shirt", currency: "GBP", cost: 13 }, { item: "Jacket", currency: "CAD", cost: 150 } } ]

使用端点,例如:/ continents?currency = EUR

我要过滤这样的结果

[ { id: 1, name: "Person 1", boughtItems: { item: "Bag", currency: "EUR", cost: 300 } } ]

,例如:/ continents?currency = GBP

[ { id: 1, name: "Person 1", boughtItems: { item: "Shoes", currency: "GBP", cost: 200 } }, { id: 2, name: "Person 2", boughtItems: { item: "Shirt", currency: "GBP", cost: 13 } } ]

我应该使用过滤器方法来做到这一点吗?

2 个答案:

答案 0 :(得分:1)

您的Javascript数组无效。如果确实是您的意思,boughtItems应该是列表,我修改了数组。

这不会更改原始数据

使用Array#reduceArray#filter

const data = [{
    id: 1,
    name: "Person 1",
    boughtItems: [{
        item: "Shoes",
        currency: "GBP",
        cost: 200
      },
      {
        item: "Bag",
        currency: "EUR",
        cost: 300
      }
    ]
  },
  {
    id: 2,
    name: "Person 2",
    boughtItems: [{
        item: "Shirt",
        currency: "GBP",
        cost: 13
      },
      {
        item: "Jacket",
        currency: "CAD",
        cost: 150
      }
    ]
  }
]

//get currency via request params
const c = "EUR";

const res = data.reduce((acc, {boughtItems, id, name}) => {
  //filter through all items, and get those that match currency
  const items = boughtItems.filter(({currency}) => currency === c);

  //if there were some that matched, create the object with the items that match
  if (items.length > 0) {
    acc.push({id,name,boughtItems: items})
  }

  return acc;
}, []);

console.log(res);

这里是功能:

const data = [{
    id: 1,
    name: "Person 1",
    boughtItems: [{
        item: "Shoes",
        currency: "GBP",
        cost: 200
      },
      {
        item: "Bag",
        currency: "EUR",
        cost: 300
      }
    ]
  },
  {
    id: 2,
    name: "Person 2",
    boughtItems: [{
        item: "Shirt",
        currency: "GBP",
        cost: 13
      },
      {
        item: "Jacket",
        currency: "CAD",
        cost: 150
      }
    ]
  }
]

function search(c){
    return data.reduce((acc, {boughtItems, id, name}) => {
      //filter through all items, and get those that match currency
      const items = boughtItems.filter(({currency}) => currency === c);

      //if there were some that matched, create the object with the items that match
      if (items.length > 0) {
        acc.push({id,name,boughtItems: items})
      }

      return acc;
    }, []);
}

console.log(search("EUR"));
console.log(search("GBP"));

答案 1 :(得分:1)

只需使用filter

const array = [
    {
      id: 1,
      name: "Person 1",
      boughtItems: [{
        item: "Shoes",
        currency: "GBP",
        cost: 200
      },
      {
        item: "Bag",
        currency: "EUR",
        cost: 300
      }]
    },
      {
      id: 2,
      name: "Person 2",
      boughtItems: [{
        item: "Shirt",
        currency: "GBP",
        cost: 13
      },
      {
        item: "Jacket",
        currency: "CAD",
        cost: 150
      }]
      }
    ];

const filterData = (array, filterValue) => array.filter(obj => (obj.boughtItems = obj.boughtItems.filter(o => o.currency === filterValue)).length);

console.log(filterData(array, 'GBP'));