1

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.

4
  • You only want one transaction? On your expected output, you only removed the 2nd transaction. Commented Apr 13, 2018 at 2:59
  • no, but want to transaction which have same purchase id value. Commented Apr 13, 2018 at 3:02
  • which property is purchase id value? purchase or _id? Commented Apr 13, 2018 at 3:06
  • yes, purchase, not _id. Commented Apr 13, 2018 at 3:16

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.