Reputation: 12729
I am getting response from service like this
var obj = {
"master": [
{
"id": 1,
"abc": [
{
"id": 1,
"categoryId": 1,
"displayName": "Prepaid",
"value": "PREPAID"
},
{
"id": 2,
"categoryId": 1,
"displayName": "Prepaid CYN",
"value": "PREPAID_CYN"
}
],
"name": "Product Type",
"value": "productType"
},
{
"id": 2,
"abc": [
{
"id": 6,
"categoryId": 2,
"displayName": "Mobile",
"value": "Mobile"
}
],
"name": "Criteria",
"value": "criteria"
},
{
"id": 3,
"abc": [
{
"id": 7,
"categoryId": 3,
"displayName": "Card",
"value": "Aasssar"
},
{
"id": 8,
"categoryId": 3,
"displayName": "Driving",
"value": "li"
}
],
"name": "Proof",
"value": "Proof"
}
]
}
let proofArr=[],
productType=[];
for(var i=0;obj.master.length;i++){
console.log(obj)
if(obj[i].master[i].value ==='productType'){
productType = obj[i].master[i].abc;
}
}
for(var i=0;obj.master.length;i++){
if(obj[i].master[i].value ==='product type'){
proofArr = obj[i].master[i].abc;
}
}
console.log(productType)
console.log(proofArr)
I want to transform or get array from response.
expected output
productType=
[
{
"id": 1,
"categoryId": 1,
"displayName": "Prepaid",
"value": "PREPAID"
},
{
"id": 2,
"categoryId": 1,
"displayName": "Prepaid CYN",
"value": "PREPAID_CYN"
}
]
proofArr =
[
{
"id": 7,
"categoryId": 3,
"displayName": "Card",
"value": "Aasssar"
},
{
"id": 8,
"categoryId": 3,
"displayName": "Driving",
"value": "li"
}
]
how I will get data from response ?
Upvotes: 1
Views: 5955
Reputation: 788
Problems in your code:
var obj = {
"master": [{
"id": 1,
"abc": [{
"id": 1,
"categoryId": 1,
"displayName": "Prepaid",
"value": "PREPAID"
},
{
"id": 2,
"categoryId": 1,
"displayName": "Prepaid CYN",
"value": "PREPAID_CYN"
}
],
"name": "Product Type",
"value": "productType"
},
{
"id": 2,
"abc": [{
"id": 6,
"categoryId": 2,
"displayName": "Mobile",
"value": "Mobile"
}],
"name": "Criteria",
"value": "criteria"
},
{
"id": 3,
"abc": [{
"id": 7,
"categoryId": 3,
"displayName": "Card",
"value": "Aasssar"
},
{
"id": 8,
"categoryId": 3,
"displayName": "Driving",
"value": "li"
}
],
"name": "Proof",
"value": "Proof"
}
]
}
let proofArr = [],
productType = [];
for (var i = 0; i < obj.master.length; i++) {
if (obj.master[i].value === 'productType') {
productType = obj.master[i].abc;
}
if (obj.master[i].value === 'Proof') {
proofArr = obj.master[i].abc;
}
}
console.log(productType)
console.log(proofArr)
Upvotes: 2
Reputation: 386560
You could filter the wanted arrays by value
and reduce all found arrays to a single array.
function getByType(type) {
return obj.master.reduce(
(r, { value, abc }) => r.concat(value === type ? abc : []),
[]
);
}
var obj = { master: [{ id: 1, abc: [{ id: 1, categoryId: 1, displayName: "Prepaid", value: "PREPAID" }, { id: 2, categoryId: 1, displayName: "Prepaid CYN", value: "PREPAID_CYN" }], name: "Product Type", value: "productType" }, { id: 2, abc: [{ id: 6, categoryId: 2, displayName: "Mobile", value: "Mobile" }], name: "Criteria", value: "criteria" }, { id: 3, abc: [{ id: 7, categoryId: 3, displayName: "Card", value: "Aasssar" }, { id: 8, categoryId: 3, displayName: "Driving", value: "li" }], name: "Proof", value: "Proof" }] };
console.log(getByType('productType'));
console.log(getByType('Proof'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0
Reputation: 26844
Another option you have is to use find()
to search for the first match.
var obj = {"master":[{"id":1,"abc":[{"id":1,"categoryId":1,"displayName":"Prepaid","value":"PREPAID"},{"id":2,"categoryId":1,"displayName":"Prepaid CYN","value":"PREPAID_CYN"}],"name":"Product Type","value":"productType"},{"id":2,"abc":[{"id":6,"categoryId":2,"displayName":"Mobile","value":"Mobile"}],"name":"Criteria","value":"criteria"},{"id":3,"abc":[{"id":7,"categoryId":3,"displayName":"Card","value":"Aasssar"},{"id":8,"categoryId":3,"displayName":"Driving","value":"li"}],"name":"Proof","value":"Proof"}]}
var productType = (obj.master.find(o => o.value === "productType") || {abc: []}).abc;
var proofArr = (obj.master.find(o => o.value === "Proof") || {abc: []}).abc;
console.log( productType );
console.log( proofArr );
Upvotes: 4