J'essaie de filtrer le tableau que les résultats de la valeur id existent dans le tableau de l'id.

Ici la variable de données:

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"
    }
  ]

Variable id sélectionnée:

var ids = [
"5acb747c9c2be225d995a8f1",
"5acb747c9c2be225d995a8f2"
]

Fonction de filtrage:

var filter = function(){
  for (id of ids){
    for (dataTransaksi of data){
        console.log(dataTransaksi.transaksi.filter(transaksi => {
        return transaksi._id == id
        }))
    }
  }
}
filter()

Résultat attendu:

[
    {
        "transaksi": [
            {
                "_id": "5acb747c9c2be225d995a8f1",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 50000,
                "kirim": 1,
                "pembelian": "5acb747c9c2be225d995a8f9"
            }
          ]
    },
    {
        "transaksi": [
            {
                "_id": "5acb747c9c2be225d995a8f2",
                "sebelum_jumlah_sortir": 1,
                "harga_produk": 20000,
                "kirim": 1,
                "pembelian": "5acb747c9c2be225d995a8f9"
            }
          ]
    }
 ]

Un résultat réel inclut vide et divisé: entrez la description de l'image ici

Merci.

0
Dary 11 avril 2018 à 11:32

4 réponses

Meilleure réponse

Vous pouvez utiliser .filter() avec Object.assign() comme ceci:

Démo:

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; }
1
Mohammad Usman 11 avril 2018 à 08:39

Vous ne poussez pas les objets trouvés dans le jeu de résultats.

function filter() {
    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; }

Une version plus courte.

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; }
1
Nina Scholz 11 avril 2018 à 08:39
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);
0
CapitainShypoop 11 avril 2018 à 08:39

Je pense que vous pouvez essayer comme ceci:

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);
1
mystic_coder 11 avril 2018 à 09:25