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 ?
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 ?
Share Improve this question asked Aug 28, 2018 at 9:32 user944513user944513 12.8k52 gold badges185 silver badges348 bronze badges 4- You need to format the data from the service to how you like... Have you tried that ? – Pogrindis Commented Aug 28, 2018 at 9:35
-
Have you used
Object.entries()
? – Blundering Philosopher Commented Aug 28, 2018 at 9:37 -
Also
obj
is not an array, so itterating over it with for loop doesnt make sense.. useobj.master[i]
jsfiddle/ce01rs8q/2 – Pogrindis Commented Aug 28, 2018 at 9:39 - @Pogrindis we are using two loops , can we optimise this – user944513 Commented Aug 28, 2018 at 9:42
3 Answers
Reset to default 4Another 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 );
Problems in your code:
- bad condition (totally missing the counter i)
- trying to access obj[i].master instead obj.master
- bad condition when you are loading the proof array
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)
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; }