i have a json. i need to copy the content of "data" if provider is "AXIS DATA" to a new json file. How?
what i have tried is
first i convert with json.parse
and searching it with for loop.
this is my code so far
var hasilsearching = {};
var hasildata = JSON.parse( FIRST JSON );
for (var i=0 ; i < hasildata.data.length ; i++){
if (hasildata.data[i].provider == "AXIS DATA") {
hasilsearching = hasildata.data[i];
}
}
but not what i expected. I know inside the if()
is the problem. but i dont know.
this is my json file
{
"errNumber": "0",
"userID": "EKL0003097",
"data": [
{
"code": "BXD1",
"price": "15000.00",
"name": "Voucher Axis Aigo 1GB 24J 30H",
"ep": "770",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BXD2",
"price": "25000.00",
"name": "Voucher Axis Aigo 2GB 24J 30H",
"ep": "660",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BOLT1",
"price": "31000.00",
"name": "Bolt Kuota 1,5GB 24Jam 30hr",
"ep": "1320",
"isActive": "Active",
"type": "KUOTA",
"provider": "BOLT"
},
{
"code": "BOLT3",
"price": "50000.00",
"name": "Bolt Kuota 3GB 24Jam 30hr",
"ep": "1127",
"isActive": "Active",
"type": "KUOTA",
"provider": "BOLT"
}
],
"respMessage": "PROSES BERHASIL"
}
this is expected new json file
{
"data": [
{
"code": "BXD1",
"price": "15000.00",
"name": "Voucher Axis Aigo 1GB 24J 30H",
"ep": "770",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BXD2",
"price": "25000.00",
"name": "Voucher Axis Aigo 2GB 24J 30H",
"ep": "660",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
}
]
}
Add an empty array to the data
property of hasilsearching
, and push
any matches on:
var hasildata = {"errNumber":"0","userID":"EKL0003097","data":[{"code":"BXD1","price":"15000.00","name":"Voucher Axis Aigo 1GB 24J 30H","ep":"770","isActive":"Active","type":"KUOTA","provider":"AXIS DATA"},{"code":"BXD2","price":"25000.00","name":"Voucher Axis Aigo 2GB 24J 30H","ep":"660","isActive":"Active","type":"KUOTA","provider":"AXIS DATA"},{"code":"BOLT1","price":"31000.00","name":"Bolt Kuota 1,5GB 24Jam 30hr","ep":"1320","isActive":"Active","type":"KUOTA","provider":"BOLT"},{"code":"BOLT3","price":"50000.00","name":"Bolt Kuota 3GB 24Jam 30hr","ep":"1127","isActive":"Active","type":"KUOTA","provider":"BOLT"}],"respMessage":"PROSES BERHASIL"};
var hasilsearching = { data: [] };
for (var i=0 ; i < hasildata.data.length ; i++){
if (hasildata.data[i].provider == "AXIS DATA") {
hasilsearching.data.push(hasildata.data[i]);
}
}
console.log(hasilsearching);
.as-console-wrapper { max-height: 100% !important; top: auto; }
It's also much easier to use filter
:
var hasildata = {"errNumber":"0","userID":"EKL0003097","data":[{"code":"BXD1","price":"15000.00","name":"Voucher Axis Aigo 1GB 24J 30H","ep":"770","isActive":"Active","type":"KUOTA","provider":"AXIS DATA"},{"code":"BXD2","price":"25000.00","name":"Voucher Axis Aigo 2GB 24J 30H","ep":"660","isActive":"Active","type":"KUOTA","provider":"AXIS DATA"},{"code":"BOLT1","price":"31000.00","name":"Bolt Kuota 1,5GB 24Jam 30hr","ep":"1320","isActive":"Active","type":"KUOTA","provider":"BOLT"},{"code":"BOLT3","price":"50000.00","name":"Bolt Kuota 3GB 24Jam 30hr","ep":"1127","isActive":"Active","type":"KUOTA","provider":"BOLT"}],"respMessage":"PROSES BERHASIL"};
var hasilsearching = { data: hasildata.data.filter(({ provider }) => provider == "AXIS DATA") };
console.log(hasilsearching);
.as-console-wrapper { max-height: 100% !important; top: auto; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With