如何按对象值过滤数组并返回一个新数组?

时间:2018-03-19 01:49:52

标签: javascript arrays object filtering

我很难找到一种通过id值过滤这个数组的方法,并生成一个新的保留coord的过滤器。

数组示例:

var herbs =[
{
"coords":[3300,2796],"items":[
{id: "dandelion",qty: 72},
{id: "sage",qty: 4},
{id: "valerian",qty: 1},
]},
{
"coords":[3300,2800],"items":[
{id: "dandelion",qty: 26},
{id: "valerian",qty: 7},
{id: "sage",qty: 2},
]},
{
"coords":[3300,2804],"items":[
{id: "dandelion",qty: 57},
{id: "sage",qty: 4},
{id: "wormwood",qty: 1},
]}]

我想通过id过滤它,用它的coords生成一个新的。

示例:

id = dandelion

过滤
var dandelion =[
{
"coords":[3300,2796],"items":[
{id: "dandelion",qty: 72},
]},
{
"coords":[3300,2800],"items":[
{id: "dandelion",qty: 26},
]},
{
"coords":[3300,2804],"items":[
{id: "dandelion",qty: 57},
]}]

id = sage

过滤
var sage =[
{
"coords":[3300,2796],"items":[
{id: "sage",qty: 4},
]},
{
"coords":[3300,2800],"items":[
{id: "sage",qty: 2},
]},
{
"coords":[3300,2804],"items":[
{id: "sage",qty: 4},
]}]

另外,这个阵列非常大,我有467.000个坐标。所以我打算过滤它并保存每个过滤的新文件。

3 个答案:

答案 0 :(得分:2)

您可以使用reduce来推送一个新数组,其中items的结果为filter。当在items

中找到搜索词时,它只会推送到新数组

var herbs =[
    {
    "coords":[3300,2796],"items":[
    {id: "dandelion",qty: 72},
    {id: "sage",qty: 4},
    {id: "valerian",qty: 1},
    ]},
    {
    "coords":[3300,2800],"items":[
    {id: "dandelion",qty: 26},
    {id: "valerian",qty: 7},
    {id: "sage",qty: 2},
    ]},
    {
    "coords":[3300,2804],"items":[
    {id: "dandelion",qty: 57},
    {id: "sage",qty: 4},
    {id: "wormwood",qty: 1},
    ]}]

function filterByID(array, id) {
    return array.reduce((a, c) => {
        let items = c.items.filter(i => i.id === id )
        if (items.length){
            a.push({
            coords: c.coords,
            items: items
            })
        }
        return a
    }, [])
}    


console.log(filterByID(herbs, "dandelion"))
console.log(filterByID(herbs, "sage"))

答案 1 :(得分:0)

非常简单。使用标准Array方法mapfilter来获得您想要的内容。



var herbs = [{
    "coords": [3300, 2796],
    "items": [{
        id: "dandelion",
        qty: 72
      },
      {
        id: "sage",
        qty: 4
      },
      {
        id: "valerian",
        qty: 1
      },
    ]
  },
  {
    "coords": [3300, 2800],
    "items": [{
        id: "dandelion",
        qty: 26
      },
      {
        id: "valerian",
        qty: 7
      },
      {
        id: "sage",
        qty: 2
      },
    ]
  },
  {
    "coords": [3300, 2802],
    "items": [{
        id: "dandelion",
        qty: 26
      },
      {
        id: "valerian",
        qty: 7
      },
      //no sage
    ]
  },
  {
    "coords": [3300, 2804],
    "items": [{
        id: "dandelion",
        qty: 57
      },
      {
        id: "sage",
        qty: 4
      },
      {
        id: "wormwood",
        qty: 1
      },
    ]
  }
];

function filterHerbs(id) {
  return herbs.map((h) => {
      return {
        coords: h.coords,
        items: h.items.filter((i) => i.id == id)
      }
    })//filter result in case of empty item arrays
    .filter((h) => h.items.length);
}
var dand = filterHerbs('dandelion')
console.log(dand);
var sage = filterHerbs('sage');
console.log(sage);




答案 2 :(得分:0)

const dandelion = herbs.map(
    ({coords, items}) => items.filter(
        ({id}) => id === 'dandelion'
).map(ob => ({coords, ...ob})));