最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to filter in nested array without eliminating array parent in javascript - Stack Overflow

programmeradmin2浏览0评论

i'm trying to filter array that the results of value {purchase} id contain in array of {products.transactions.purchase} id. I want to filter by transaction which have same purchase id value.

the data:

var data = [
  {
    "purchase": "5ace99b014a759325776aabb",
    "products":
      [{
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "25"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "23"
          }],
        "_id": "5ace995914a759325776aab0",
        "product_name": "Milk",
      },
      {
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "20"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "15"
          }],
        "_id": "5ace995c14a759325776aab1",
        "product_name": "Ketchup",
      }]
  },
  {
    "purchase": "5ace99d714a759325776aac4",
    "products":
      [{
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "22"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "21"
          }],
        "_id": "5ace995914a759325776aab0",
        "nama_produk": "Milk",
      },
      {
        "transactions": 
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "14"
        },
        {
          "purchase": "5ace99d714a759325776aac4",
          "price": "13"
        }],
        "_id": "5ace995c14a759325776aab1",
        "product_name": "Ketchup",
      }]
  }]

i have been tried but only show child array

function filter() {
  let result = []
  let filter = data.filter(a => {
    return a.products.filter(b => {
      return b.transactions.filter(c => {
        if (a.purchase == c.purchase) result.push(c)
      })
    })
  })
  return result
}
console.log(filter())

// output
> Array [Object 
{ purchase: "5ace99b014a759325776aabb", price: "25" }, Object
{ purchase: "5ace99b014a759325776aabb", price: "20" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "21" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "13" }]

how to the output of filtered array like below output:

[{
  "purchase": "5ace99b014a759325776aabb",
  "products":
    [{
      "transactions":
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "25"
        }],
      "_id": "5ace995914a759325776aab0",
      "product_name": "Milk",
    },
    {
      "transactions":
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "20"
        }],
      "_id": "5ace995c14a759325776aab1",
      "product_name": "Ketchup",
    }]
},
{
  "purchase": "5ace99d714a759325776aac4",
  "products":
    [{
      "transactions":
        [{
          "purchase": "5ace99d714a759325776aac4",
          "price": "21"
        }],
      "_id": "5ace995914a759325776aab0",
      "nama_produk": "Milk",
    },
    {
      "transactions":
        [{
          "purchase": "5ace99d714a759325776aac4",
          "price": "13"
        }],
      "_id": "5ace995c14a759325776aab1",
      "product_name": "Ketchup",
    }]
}]

Thank you.

i'm trying to filter array that the results of value {purchase} id contain in array of {products.transactions.purchase} id. I want to filter by transaction which have same purchase id value.

the data:

var data = [
  {
    "purchase": "5ace99b014a759325776aabb",
    "products":
      [{
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "25"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "23"
          }],
        "_id": "5ace995914a759325776aab0",
        "product_name": "Milk",
      },
      {
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "20"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "15"
          }],
        "_id": "5ace995c14a759325776aab1",
        "product_name": "Ketchup",
      }]
  },
  {
    "purchase": "5ace99d714a759325776aac4",
    "products":
      [{
        "transactions":
          [{
            "purchase": "5ace99b014a759325776aabb",
            "price": "22"
          },
          {
            "purchase": "5ace99d714a759325776aac4",
            "price": "21"
          }],
        "_id": "5ace995914a759325776aab0",
        "nama_produk": "Milk",
      },
      {
        "transactions": 
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "14"
        },
        {
          "purchase": "5ace99d714a759325776aac4",
          "price": "13"
        }],
        "_id": "5ace995c14a759325776aab1",
        "product_name": "Ketchup",
      }]
  }]

i have been tried but only show child array

function filter() {
  let result = []
  let filter = data.filter(a => {
    return a.products.filter(b => {
      return b.transactions.filter(c => {
        if (a.purchase == c.purchase) result.push(c)
      })
    })
  })
  return result
}
console.log(filter())

// output
> Array [Object 
{ purchase: "5ace99b014a759325776aabb", price: "25" }, Object
{ purchase: "5ace99b014a759325776aabb", price: "20" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "21" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "13" }]

how to the output of filtered array like below output:

[{
  "purchase": "5ace99b014a759325776aabb",
  "products":
    [{
      "transactions":
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "25"
        }],
      "_id": "5ace995914a759325776aab0",
      "product_name": "Milk",
    },
    {
      "transactions":
        [{
          "purchase": "5ace99b014a759325776aabb",
          "price": "20"
        }],
      "_id": "5ace995c14a759325776aab1",
      "product_name": "Ketchup",
    }]
},
{
  "purchase": "5ace99d714a759325776aac4",
  "products":
    [{
      "transactions":
        [{
          "purchase": "5ace99d714a759325776aac4",
          "price": "21"
        }],
      "_id": "5ace995914a759325776aab0",
      "nama_produk": "Milk",
    },
    {
      "transactions":
        [{
          "purchase": "5ace99d714a759325776aac4",
          "price": "13"
        }],
      "_id": "5ace995c14a759325776aab1",
      "product_name": "Ketchup",
    }]
}]

Thank you.

Share Improve this question edited Apr 13, 2018 at 3:03 Pengguna asked Apr 13, 2018 at 2:55 PenggunaPengguna 4,9591 gold badge30 silver badges33 bronze badges 4
  • You only want one transaction? On your expected output, you only removed the 2nd transaction. – Eddie Commented Apr 13, 2018 at 2:59
  • no, but want to transaction which have same purchase id value. – Pengguna Commented Apr 13, 2018 at 3:02
  • which property is purchase id value? purchase or _id? – CozyAzure Commented Apr 13, 2018 at 3:06
  • yes, purchase, not _id. – Pengguna Commented Apr 13, 2018 at 3:16
Add a ment  | 

2 Answers 2

Reset to default 4

You can use map and filter

let result = data.map(o=>{ //Use map to loop thru the array
    o = Object.assign({},o);   //Clone the object. So any changes will not affect the original array.
    o.products.map(p=>{   //Use map to loop thru the products
        p.transactions = p.transactions.filter(v=>v.purchase === o.purchase);  //Filter the transactions of the products
    });
    return o;
});

Here is a snippet:

var data=[{purchase:"5ace99b014a759325776aabb",products:[{transactions:[{purchase:"5ace99b014a759325776aabb",price:"25"},{purchase:"5ace99d714a759325776aac4",price:"23"}],_id:"5ace995914a759325776aab0",product_name:"Milk"},{transactions:[{purchase:"5ace99b014a759325776aabb",price:"20"},{purchase:"5ace99d714a759325776aac4",price:"15"}],_id:"5ace995c14a759325776aab1",product_name:"Ketchup"}]},{purchase:"5ace99d714a759325776aac4",products:[{transactions:[{purchase:"5ace99b014a759325776aabb",price:"22"},{purchase:"5ace99d714a759325776aac4",price:"21"}],_id:"5ace995914a759325776aab0",nama_produk:"Milk"},{transactions:[{purchase:"5ace99b014a759325776aabb",price:"14"},{purchase:"5ace99d714a759325776aac4",price:"13"}],_id:"5ace995c14a759325776aab1",product_name:"Ketchup"}]}];

let result = data.map(o => {
  o = Object.assign({}, o);
  o.products.map(p => {
    p.transactions = p.transactions.filter(v => v.purchase === o.purchase);
  });
  return o;
});

console.log(result);

It appears that filter is being used as a forEach here. I have rewritten the filter function to provide the correct output:

function filter() {
  return data.map(dataItem => ({
    ...dataItem,
    products: dataItem.products.map(product => ({
      ...product,
      transactions: product.transactions.filter(
        transaction => transaction.purchase === dataItem.purchase
      )
    }))
  }));
}

The implementation in the question only pushed the child items to a result array.

In Javascript, Array.prorotype.filter is generally used to produce an Array that conditionally includes or exclude items based on the truthiness of the callback, as documented here.

发布评论

评论列表(0)

  1. 暂无评论