我正在尝试过滤数组,其中value id的结果存在于id数组中。
这里是数据变量:
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"
}
]
选择的id变量:
var ids = [
"5acb747c9c2be225d995a8f1",
"5acb747c9c2be225d995a8f2"
]
过滤功能:
var filter = function(){
for (id of ids){
for (dataTransaksi of data){
console.log(dataTransaksi.transaksi.filter(transaksi => {
return transaksi._id == id
}))
}
}
}
filter()
预期结果:
[
{
"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"
}
]
}
]
谢谢。
答案 0 :(得分:1)
您可以.filter()
使用Object.assign()
,如下所示:
<强>演示:强>
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 1 :(得分:1)
您不会将找到的对象推送到结果集。
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());
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
较短的版本。
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 2 :(得分:1)
我想你可以尝试这样:
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);
答案 3 :(得分:0)
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);
&#13;