Skip to content Skip to sidebar Skip to footer

Javascript Unexpected Include Splitted And Empty Array Filter Output

i'm trying to filter array that the results of value id exist in array of id. here the data variable: var data = [ { 'transaksi': [ { '_id'

Solution 1:

You can use .filter() with Object.assign() like this:

Demo:

let data = [{"transaksi": [{"_id": "5acb747c9c2be225d995a8f1", "sebelum_jumlah_sortir": 1, "harga_produk": 50000,"kirim": 1,"pembelian": "5acb747c9c2be225d995a8f9"}, { "_id": "5acb7a6a305ef72b7d542900", "sebelum_jumlah_sortir": 1,"harga_produk": 50000,"kirim": 1,"pembelian": "5acb7a6a305ef72b7d542908"}], "_id": "5acb74239c2be225d995a8ee", "nama_produk": "Susu"},{ "transaksi": [{"_id": "5acb747c9c2be225d995a8f2", "sebelum_jumlah_sortir": 1,"harga_produk": 20000, "kirim": 1,"pembelian": "5acb747c9c2be225d995a8f9"},{"_id": "5acb7a6a305ef72b7d542901","sebelum_jumlah_sortir": 1,"harga_produk": 20000,"kirim": 1, "pembelian": "5acb7a6a305ef72b7d542908"}], "_id": "5acb74279c2be225d995a8ef", "nama_produk": "Remot Tv"}],
    ids = ["5acb747c9c2be225d995a8f1", "5acb747c9c2be225d995a8f2"];

let result = data.map(
  o =>Object.assign(
    {}, o, {"transaksi": o["transaksi"].filter(o => ids.includes(o["_id"]))}
  )
);

console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 2:

You do not push the found objects to the result set.

functionfilter() {
    var result = [],
        id, dataTransaksi;

    for (id of ids) {
        for (dataTransaksi of data){
            result.push(...dataTransaksi.transaksi.filter(transaksi => {
                return transaksi._id == id
            }));
        }
    }
    return result;
}

var data = [{ transaksi: [{ _id: "5acb747c9c2be225d995a8f1", sebelum_jumlah_sortir: 1, harga_produk: 50000, kirim: 1, pembelian: "5acb747c9c2be225d995a8f9" }, { _id: "5acb7a6a305ef72b7d542900", sebelum_jumlah_sortir: 1, harga_produk: 50000, kirim: 1, pembelian: "5acb7a6a305ef72b7d542908" }], _id: "5acb74239c2be225d995a8ee", nama_produk: "Susu" }, { transaksi: [{ _id: "5acb747c9c2be225d995a8f2", sebelum_jumlah_sortir: 1, harga_produk: 20000, kirim: 1, pembelian: "5acb747c9c2be225d995a8f9" }, { _id: "5acb7a6a305ef72b7d542901", sebelum_jumlah_sortir: 1, harga_produk: 20000, kirim: 1, pembelian: "5acb7a6a305ef72b7d542908" }], _id: "5acb74279c2be225d995a8ef", nama_produk: "Remot Tv" }],
    ids = ["5acb747c9c2be225d995a8f1", "5acb747c9c2be225d995a8f2"];

console.log(filter());
.as-console-wrapper { max-height: 100%!important; top: 0; }

A shorter version.

var data = [{ transaksi: [{ _id: "5acb747c9c2be225d995a8f1", sebelum_jumlah_sortir: 1, harga_produk: 50000, kirim: 1, pembelian: "5acb747c9c2be225d995a8f9" }, { _id: "5acb7a6a305ef72b7d542900", sebelum_jumlah_sortir: 1, harga_produk: 50000, kirim: 1, pembelian: "5acb7a6a305ef72b7d542908" }], _id: "5acb74239c2be225d995a8ee", nama_produk: "Susu" }, { transaksi: [{ _id: "5acb747c9c2be225d995a8f2", sebelum_jumlah_sortir: 1, harga_produk: 20000, kirim: 1, pembelian: "5acb747c9c2be225d995a8f9" }, { _id: "5acb7a6a305ef72b7d542901", sebelum_jumlah_sortir: 1, harga_produk: 20000, kirim: 1, pembelian: "5acb7a6a305ef72b7d542908" }], _id: "5acb74279c2be225d995a8ef", nama_produk: "Remot Tv" }],
    ids = ["5acb747c9c2be225d995a8f1", "5acb747c9c2be225d995a8f2"],
    result = data.reduce(
        (r, { transaksi }) => r.concat(transaksi.filter(({ _id }) => ids.includes(_id))),
        []
    );

console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 3:

I think you can try like this :

var filter = function(){
  let result =  data.filter((transcript) =>
  {
    let found = false;
    for(ts of transcript.transaksi)
    {
        //console.log(ids.indexOf(ts._id));if(ids.indexOf(ts._id) !== -1)
        {
            found = true;
            break;
        }
    }
    //console.log(found);return found;
  });

  return result;
}
let result = filter();
console.log(result);

Solution 4:

data.filter(d => ids.indexOf(d._id) == -1)

var data = [
    {
        "transaksi": [
            {
                "_id": "5acb747c9c2be225d995a8f1",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 50000,
                "kirim": 1,
                "pembelian": "5acb747c9c2be225d995a8f9"
            },
            {
                "_id": "5acb7a6a305ef72b7d542900",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 50000,
                "kirim": 1,
                "pembelian": "5acb7a6a305ef72b7d542908"
            }
        ],
        "_id": "5acb74239c2be225d995a8ee",
        "nama_produk": "Susu"
    },
    {
        "transaksi": [
            {
                "_id": "5acb747c9c2be225d995a8f2",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 20000,
                "kirim": 1,
                "pembelian": "5acb747c9c2be225d995a8f9"
            },
            {
                "_id": "5acb7a6a305ef72b7d542901",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 20000,
                "kirim": 1,
                "pembelian": "5acb7a6a305ef72b7d542908"
            }
        ],
        "_id": "5acb74279c2be225d995a8ef",
        "nama_produk": "Remot Tv"
    }
  ]
  
  
  
 var ids = [
"5acb747c9c2be225d995a8f1",
"5acb747c9c2be225d995a8f2"
]


let res = data.filter(d => ids.indexOf(d._id) == -1)


console.log(res);

Post a Comment for "Javascript Unexpected Include Splitted And Empty Array Filter Output"